Is it possible to hold symvars, if the value is 0?

Hi all,
I’ve a question concerning the symbolic package.
With the function_handle -function it’s possible to transfer a symbolic function into
a usual octave-function.

Suppose a vectorfunction like

  f=[sx1,sx2.^2]

there are no problems:

  f(sx1,sx2)=[sx1;sx.^2]

But supposing the symfcn

 f=[sx1;0*sx1],

the function_handle gives

 f(sx1)=[sx1;0].

So it leads to an error if calling f(1:10), 'cause the dim varies.
I can’t believe, that there is no solution to this problem.

I’ve make a scary workaround: If there a out-dim without a domain-var, i added sym(1)/10^15 to the vectorfunction. It’s not just scary, but also change the functionresult too.

I can change to functions like:

     f=functionhandle(sx2,-sx1)

whats lead to

 [y1,y2]=f(sx1,sx2),

but thats no Option for my purpose.

Any suggestions?

Today, i’m happy of a solution i’ve found.
I turn the arrayfcn into a cellfcn:

    f_h_Cell=@(x, y) {x .* y, 0}
    
   nf = @(x) @mifCell2Mat (f_h_Cell (x (size (x) (1) * 0 / 2 + 1:size (x) (1) * 1 / 2,{':'} (ones (1, ndims (x) - 1)){:}), x (size (x) (1) * 1 / 2 + 1:size (x) (1) * 2 / 2, {':'} (ones (1, ndims (x) - 1)) {:})))

and then:

function res=mifCell2Mat(resCell)
  resCell=transpose(resCell);
  [~,idx]=max(cellfun(@numel,resCell));
  refSize=(size(resCell{idx}));
  resCell=cellfun(@(x) x+zeros(refSize),resCell,'uniformoutput',false);
  res=cell2mat(resCell);
endfunction

All automate calling following function
f=fcn(name,domain,parms,fcn);

so a simple f.nf([x;y;z]) call gives the result.

Of course it doesn’t work, if there are numel’s between 1 and say size=[10,10] of
eg size=[10,1], but so what … In most cases it work’s for me (until now: allways).

Oh, while i read my code just here, i’ve found a little bug:

refSize=(size(resCell{idx}));

must of course change to

refSize=(size(resCell{idx(1)})); 

cause there a possible more than one max sizes in the idx, so i’ve picked the first. I do first a test of constant outDims, so that these workaround only occours, if there are constants. In the other cases (if all outDims contain domainvars) a simple anonymous function of a matrix-handle appears to the user:

f_h_Mat=@(x, y) [x .* y; x]

nf=@(x) f_h_Mat (x (size (x) (1) * 0 / 2 + 1:size (x) (1) * 1 / 2, {':'} (ones (1, ndims (x) - 1)) {:}), x (size
(x) (1) * 1 / 2 + 1:size (x) (1) * 2 / 2, {':'} (ones (1, ndims (x) - 1)) {:}))

A warning appears because the function_handle - function gets a ‘cell’ class instead of a ‘sym’:

warning: expected output expressions to be syms
warning: called from
    codegen>codegen_input_parser at line 184 column 7
    codegen at line 21 column 40
    function_handle at line 94 column 14

You can supress this with:


warning('off', 'expected output expressions to be syms');

Edit: Should work, but does not.
[msg,id]=lastwarn()
also gives no id
With id, you can suppress warnings with
warnings('off',id);

All of the tests i made suggests that there is no really problem regarding this warning.

I don’t have any suggestions to add here. It’s true that what you want to do doesn’t seem to work correctly, but that just looks like a limitation of converting between symbolic expressions and vectorized expressions.

If your second comment is a solution to the problem, you can mark it as a solution for this topic.