How to apply functions to symmatrix objects, in the same way as to sym objects.

Hi
I work with symbolic matrix objects, defined as symmatrix.
Unfortunatly, symmatrix is not compatible with as many functions as a symbolic scalar (sym). In my case i would like to apply normcdf or equivalently erf to a symmatrix matrix (as an elementwise operation, like it could be done with a double matrix). This cant be done.
% works for scalars
syms y x;
a=erf(y*x);
b=normcdf(y*x);
diff(a,x)
ans = 
diff(b,x)
ans = 
% fails for symmatrix
Y = symmatrix('Y',[2,1]);
syms x
a=erf(Y*x)
Incorrect number or types of inputs or outputs for function erf.
b=normcdf(Y*x)
diff(a,x)
diff(b,x)
I am wondering if there is a workaround via an abstract function. I thought maybe symfunmatrix helps, but I couldnt get it to do what I want.
Importantly, I dont want any conversion to sym, because I want to later use the resulting matrix expressions for numeric evaluation for arbitrary n. Conversion to sym would turn matrix expressions into element by element operatons and make the numeric evaluation slow.

6 Comments

No one has responded. Just wondering if that is because I didnt explain myself well, or if that is really an unsually hard or uninteresting question. If there is anything I can do to imporve the question let me know. Thanks!
Hi dominik,
Y = symmatrix('Y',[2,1]);
syms x
a = erf(Y*x)
erf is a function of a scalar input, but Y*x is a vector. So I'm not clear on what 'a' is supposed to be, if not a vector that is the elementwise operation of erf on a vector input. Can you show mathematically what you're trying do (latex equations can be entered using the Sigma icon on the insert menu, or upload a .png with handrwritend stuff using the paperclip icon on the Insert menu)?
Hi (again) Paul
yes, i want erf to be applied element by element, exactly as you say.
Just like i can do with exp or log with symmatrix objects. Or like i can do with erf with numeric matrices.
I thought symfunmatrix was meant for that, but I didnt manage to use it.
Thanks!
Dominik
If scalar math is needed, as implied by "element by element", then I don't understand why symfunmatrix would be thought to come into the problem at all. Returning to your example
syms y x;
a = erf(y*x);
da = diff(a,x)
da = 
What do we want to do with that result? Based on the code above, maybe something like this?
syms Y [2 1]
da = subs(da,y,Y)
da = 
But I'm not sure of the utility of this expression for da, so maybe that's not what you want.
The above is just a simple example. Ultimatley, I want to be able to take the jacobian of a system of equations f(Y,x)=0 where Y is a matrix, x a scalar and f(Y,x) is a vector: diff(f,[x Y]) The funtional form of f(x,Y) should be general enough to accomodate matrix operations (such as *) and elementwise operations (such as .*, epx or erf). .* and exp work, but erf for example doesn't. Generic functions g(x,Y), which could serve as a workaround for the absence of functions such as erf, also don't seem to work.
Importantly, I want symmatrix matrices and not elementwise syms matrices such as in your example. Subs could have been a workaround but it fails too.
syms y x;
a = erf(y*x);
da = diff(a,x)
da = 
Y = symmatrix('Y',[2,1]); %this is the only line different from your example above
da = subs(da,y,Y)
Error using symmatrix (line 37)
Unable to convert the argument to class 'symmatrix'.

Error in symbolic.mixin.symbolicmatrix/subs (line 353)
Y = symmatrix(Y);
o.k., now I better understand what you're trying to do
syms x
syms Y [2 1] matrix
z = exp(Y*x)
z = 
diff(z,x)
ans = 
diff(z,Y)
ans = 
I guess that works for exp but not for erf because the former is a method of the symmatrix class and the latter is not
ismember({'exp','erf'},methods(z))
ans = 1x2 logical array
1 0
I see what you're trying to do by defining everything with scalar sym variables, but as yet I don't see a way to "expand" a sym expression into a symmatrix. Maybe there's a way to do that by parsing the expression into its constituent operators and operands, subbing sym for symmatrix, and then recombining it all.
You may want to consider submitting an enhancement request to add erf to the symmatrix class.
Or maybe someone else can figure out a way to do this ....

Sign in to comment.

Answers (0)

Products

Release

R2023a

Asked:

on 11 Apr 2024

Commented:

on 24 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!