Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4215 > unrolled thread

Pattern in immediate definition

Started byeLVa <elvadrias@gmail.com>
First post2011-08-08 08:23 +0000
Last post2011-08-11 07:25 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Pattern in immediate definition eLVa <elvadrias@gmail.com> - 2011-08-08 08:23 +0000
    Re: Pattern in immediate definition "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-08-09 11:21 +0000
    Re: Pattern in immediate definition Peter Pein <petsie@dordos.net> - 2011-08-09 11:28 +0000
    Re: Pattern in immediate definition Simon <simonjtyler@gmail.com> - 2011-08-11 07:25 +0000

#4215 — Pattern in immediate definition

FromeLVa <elvadrias@gmail.com>
Date2011-08-08 08:23 +0000
SubjectPattern in immediate definition
Message-ID<j1o6dl$4i4$1@smc.vnet.net>
Hi,

I want to write more clearly the following definition :
TestFunction[{{a_, b_, c_}, {d_, e_, f_}, {g_, h_, k_}}] =
(* immediate definition with = *)
 Module[{F, det},
  F = {{a, b, c}, {d, e, f}, {g, h, k}};
  det = Det[F];
  Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
  ]

After that, I can pass any 3x3 matrix to the function and get the
result directly replaced, without having to evaluate the derivatives
again , i.e I don't want it to be written :

TestFunction[F_] :=
(* delayed definition with := *)
 Module[{det},
  det = Det[F];
  Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
  ]

for it will compute everything every time I get to call the function
(which will be inefficient since I will call it often).

However, I find the trick with the temporary F that gets to be
assigned the matrix of {{a,b,c},{d,e,f},{g,h,i}} ugly and potentially
dangerous since it uses the value of the variables (a,b,...,k) if they
are defined.

I would like something close to :
TestFunction[F_<....>] = Module[<...>] where in the first <..> I get
to specify that the argument is a 3x3 matrix and so I can have access
to F[[i,j]] (Mathematica complains about this part since F is
obviously just a symbol and not a matrix). This way I just have to
worry about F not being defined elsewhere. It will also be a cleaner
definition in my opinion.

Is that possible in any way ??
Thanks

[toc] | [next] | [standalone]


#4229

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-08-09 11:21 +0000
Message-ID<j1r588$fg4$1@smc.vnet.net>
In reply to#4215
On Mon, 08 Aug 2011 09:23:17 +0100, eLVa <elvadrias@gmail.com> wrote:

> Hi,
>
> I want to write more clearly the following definition :
> TestFunction[{{a_, b_, c_}, {d_, e_, f_}, {g_, h_, k_}}] =
> (* immediate definition with = *)
>  Module[{F, det},
>   F = {{a, b, c}, {d, e, f}, {g, h, k}};
>   det = Det[F];
>   Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
>   ]
>
> After that, I can pass any 3x3 matrix to the function and get the
> result directly replaced, without having to evaluate the derivatives
> again , i.e I don't want it to be written :
>
> TestFunction[F_] :=
> (* delayed definition with := *)
>  Module[{det},
>   det = Det[F];
>   Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
>   ]
>
> for it will compute everything every time I get to call the function
> (which will be inefficient since I will call it often).
>
> However, I find the trick with the temporary F that gets to be
> assigned the matrix of {{a,b,c},{d,e,f},{g,h,i}} ugly and potentially
> dangerous since it uses the value of the variables (a,b,...,k) if they
> are defined.
>
> I would like something close to :
> TestFunction[F_<....>] = Module[<...>] where in the first <..> I get
> to specify that the argument is a 3x3 matrix and so I can have access
> to F[[i,j]] (Mathematica complains about this part since F is
> obviously just a symbol and not a matrix). This way I just have to
> worry about F not being defined elsewhere. It will also be a cleaner
> definition in my opinion.
>
> Is that possible in any way ??
> Thanks
>

Generally it will be better to evaluate the definition explicitly if that  
is what is required rather than leaving it implicit and depending on the  
values of various globally defined symbols. Here is a straightforward and  
safe way to accomplish this while still having the benefit of using a  
delayed definition:

Block[{a, b, c, d, e, f, g, h, k, F, det},
  F = {{a, b, c}, {d, e, f}, {g, h, k}};
  det = Det[F];
  TestFunction[{{a_, b_, c_}, {d_, e_, f_}, {g_, h_, k_}}] :=
   Evaluate@Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
]

As you can see, this is nothing more than a re-ordering of your code with  
Module also being replaced by Block.

[toc] | [prev] | [next] | [standalone]


#4255

FromPeter Pein <petsie@dordos.net>
Date2011-08-09 11:28 +0000
Message-ID<j1r5ln$fkd$1@smc.vnet.net>
In reply to#4215
Am 08.08.2011 10:23, schrieb eLVa:
> Hi,
> 
> I want to write more clearly the following definition :
> TestFunction[{{a_, b_, c_}, {d_, e_, f_}, {g_, h_, k_}}] =
> (* immediate definition with = *)
>  Module[{F, det},
>   F = {{a, b, c}, {d, e, f}, {g, h, k}};
>   det = Det[F];
>   Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
>   ]
> 
> After that, I can pass any 3x3 matrix to the function and get the
> result directly replaced, without having to evaluate the derivatives
> again , i.e I don't want it to be written :
> 
> TestFunction[F_] :=
> (* delayed definition with := *)
>  Module[{det},
>   det = Det[F];
>   Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]
>   ]
> 
> for it will compute everything every time I get to call the function
> (which will be inefficient since I will call it often).
> 
> However, I find the trick with the temporary F that gets to be
> assigned the matrix of {{a,b,c},{d,e,f},{g,h,i}} ugly and potentially
> dangerous since it uses the value of the variables (a,b,...,k) if they
> are defined.
> 
> I would like something close to :
> TestFunction[F_<....>] = Module[<...>] where in the first <..> I get
> to specify that the argument is a 3x3 matrix and so I can have access
> to F[[i,j]] (Mathematica complains about this part since F is
> obviously just a symbol and not a matrix). This way I just have to
> worry about F not being defined elsewhere. It will also be a cleaner
> definition in my opinion.
> 
> Is that possible in any way ??
> Thanks
> 
> 

Hi,

for the purpose of writing a new version of TestFunction let's evaluate:
In[1]:= Block[{mat = {{a, b, c}, {d, e, f}, {g, h, k}}, det},
           det = Det@mat;
           D[det, #]& /@ Flatten[mat]
        ]
Out[1]= {-f h + e k, f g - d k, -e g + d h,
          c h - b k, -c g + a k, b g - a h,
         -c e + b f, c d - a f,-b d + a e}

Now you're ready to define (I'm tired of reformatting the copy/paste -
sorry):

TestFunction[m_]:=Block[{a,b,c,d,e,f,g,h,k},
{{a,b,c},{d,e,f},{g,h,k}}=m;
{-f h+e k,f g-d k,-e g+d h,c h-b k,-c g+a k,b g-a h,-c e+b f,c d-a f,-b
d+a e}]/;Dimensions[m]=={3,3}

In[3]:= testMat=Array[If[#1==#2,{x,y,z}[[#1]],1/(#1+#2-1)]&,{3,3}]
Out[3]= {{x,1/2,1/3},{1/2,y,1/4},{1/3,1/4,z}}

In[4]:= TestFunction[testMat]
Out[4]= {-(1/16)+y z,1/12-z/2,1/8-y/3,1/12-z/2,-(1/9)+x
z,1/6-x/4,1/8-y/3,1/6-x/4,-(1/4)+x y}

If you want the entries corresponding to numeric values of the input
matrix to be zero:

In[5]:=
TestFunc2[m_]:=Block[{a,b,c,d,e,f,g,h,k,pos},{{a,b,c},{d,e,f},{g,h,k}}=m;
pos=Position[Flatten[m],_?NumericQ]//Flatten;
ReplacePart[{-f h+e k,f g-d k,-e g+d h,c h-b k,-c g+a k,b g-a h,-c e+b
f,c d-a f,-b d+a e},Thread[pos->0]]]/;Dimensions[m]=={3,3}

In[6]:= TestFunc2[testMat]
Out[6]= {-(1/16)+y z,0,0,0,-(1/9)+x z,0,0,0,-(1/4)+x y}

I hope I didn't misunderstand your intention,

Peter

[toc] | [prev] | [next] | [standalone]


#4292

FromSimon <simonjtyler@gmail.com>
Date2011-08-11 07:25 +0000
Message-ID<j2004p$5k9$1@smc.vnet.net>
In reply to#4215
Everyone else has given the sensible answer that you should use 
Block[{a,b,...}, defn]
around your function definition.
So I'll just make a few small notes:

Your code:
F = {{a, b, c}, {d, e, f}, {g, h, k}};
det = Det[F];
Flatten[Table[D[det, F[[i, j]]], {i, 1, 3}, {j, 1, 3}]]

can be simplified to
F = {{a, b, c}, {d, e, f}, {g, h, k}};
Flatten[D[Det[F], {F}]]

and it is equivalent the flattened transpose of the adjugate (adjoint) of F, which can be calculated using Minors[F] (see the Applications part of the docs for Minors).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web