MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pattern in immediate definition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120749] Re: Pattern in immediate definition
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Tue, 9 Aug 2011 07:17:43 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201108080820.EAA04601@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

(a) Define the function BEFORE a, b, c, etc. could possibly have values  
(the first initialization cell in your notebook, permaps), or

(b) Clear them before defining the function.

Clear[testFunction, a, b, c, d, e, f, g, h, k]
testFunction[{{a_, b_, c_}, {d_, e_, f_}, {g_, h_, k_}}] =
  With[{m = {{a, b, c}, {d, e, f}, {g, h, k}}},
   With[{det = Det@m},
    Flatten@Table[D[det, m[[i, j]]], {i, 1, 3}, {j, 1, 3}]
    ]
   ]

{-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}

or

(c) more complicated than absolutely necessary, I suppose:

Clear[testFunction]
With[{$m = Partition[Array[Unique["a"] &, 9], 3]},
  With[{$p = Map[Pattern[#, Blank[]] &, $m, {2}]},
   testFunction[$p] =
    With[{$det = Det@$m},
     Flatten@Table[D[$det, $m[[i, j]]], {i, 3}, {j, 3}]]
   ]
  ]

{a11 a7 - a10 a8, -a11 a6 + a8 a9, a10 a6 - a7 a9, -a11 a4 + a10 a5,
  a11 a3 - a5 a9, -a10 a3 + a4 a9, -a5 a7 + a4 a8,
  a5 a6 - a3 a8, -a4 a6 + a3 a7}

Bobby

On Mon, 08 Aug 2011 03:20:11 -0500, eLVa <elvadrias at 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
>
>


-- 
DrMajorBob at yahoo.com




  • Prev by Date: Re: Pattern in immediate definition
  • Next by Date: Re: Random prices from FinancialData
  • Previous by thread: Pattern in immediate definition
  • Next by thread: Re: Pattern in immediate definition