Author 
Message 
mariano.suarezalvarez@gma science forum addict
Joined: 28 Apr 2006
Posts: 58

Posted: Sat Jul 01, 2006 8:43 pm Post subject:
Re: mathematica command for linearity



robert wrote:
Quote:  if I type in mathematica
f[x_+y_,z_]:=f[x_,z_]+f[y_,z_]
then when I type f[a+b,c] I get f[a,c]+f[b,c].
But then when I type f[a+b+d,c] I get f[a+b+d,c]=f[a+b+d,c] and not
f[a,c]+f[b,c]+f[d,c].
How can I teach mathematica to do that without typing a formula like the one
at the beginning?

I sually use something like the following to inform Mma
that something is linear or multilinear:
ScalarQ[x_?NumberQ] := True;
MakeLinear[f_] := Block[{},
f[x_Plus] := f/@x;
f[c_?ScalarQ] := c f[1] /; c =!= 1;
f[c_?ScalarQ x_] := c f[x];
];
MakeMultilinear[f_] := Block[{},
f[x___,y_Plus,z___] := f[x,#,z]& /@ y;
f[x___,c_?ScalarQ y_,z___] := c f[x,y,z];
f[x___,c_?ScalarQ,z___] := c f[x,1,z] /; c =!= 1;
];
For example, after those definitions, you can say:
MakeLinear[F];
F[3 x + 5 y];
and you should get
3 F[x] + 5 F[y];
If you want to make something a scalar, you have
to be explicit: for example, if you want things like
a[1], a[2] and so on to be treated as scalars, and thus
take out of (multi)linear things, you say:
ScalarQ[a[_Integer]] := True
Then
T[a[4] x]
evaluates to
a[4] T[x]
In general, these definitions alone are not good enough,
and I always end up tweaking things so that things are
more usable. Also, the patterns used above are not
general enough to cover all cases...
HTH,
 m 

Back to top 


robert1129 science forum beginner
Joined: 11 Jun 2006
Posts: 3

Posted: Sat Jul 01, 2006 8:04 pm Post subject:
mathematica command for linearity



if I type in mathematica
f[x_+y_,z_]:=f[x_,z_]+f[y_,z_]
then when I type f[a+b,c] I get f[a,c]+f[b,c].
But then when I type f[a+b+d,c] I get f[a+b+d,c]=f[a+b+d,c] and not
f[a,c]+f[b,c]+f[d,c].
How can I teach mathematica to do that without typing a formula like the one
at the beginning?
thanks in advance. 

Back to top 


Google


Back to top 



The time now is Wed Sep 19, 2018 8:11 pm  All times are GMT

