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


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

Re: Matrices as operators

Started byRay Koopman <koopman@sfu.ca>
First post2011-11-27 09:17 +0000
Last post2011-11-27 09:17 +0000
Articles 1 — 1 participant

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


Contents

  Re: Matrices as operators Ray Koopman <koopman@sfu.ca> - 2011-11-27 09:17 +0000

#6777 — Re: Matrices as operators

FromRay Koopman <koopman@sfu.ca>
Date2011-11-27 09:17 +0000
SubjectRe: Matrices as operators
Message-ID<jasv7r$764$1@smc.vnet.net>
----- Christopher Young <cy56@comcast.net> wrote:
> 
> On Nov 25, 2011, at 6:17 PM, Ray Koopman wrote:
> 
>> This is simpler.  Does it do what you want?
>> Note that 'mat' can be a ragged array.
>> Mapping at {-1} preserves its shape, whatever it may be.
>> 
>> In[1]:= op[mat_,t_] := Map[#@t&, mat, {-1}]
>> 
>> In[2]:= op[ { {Sin, Cos}, Tan, whatever }, t ]
>> 
>> Out[2]= { {Sin[t], Cos[t]}, Tan[t], whatever[t] }
> 
> 
> Checking out the Help for Map, I found that you could put tests
> in with an If[ ] that was sort of acting as a pure function.
> I think the following will always do what I want.
> Should be much faster than using While loops, too.
> 
> op2[arr_, t_] :=
>  Map[
>   If[! (NumericQ[#] || StringQ[#]),
>     #[t],
>     #
>     ] &,
>   arr,
>   {-1}
>   ]
> 
> In[38]:= op2[{{Sin, 3 E, Cos}, Tan, {Cos, Cos[Cos], 1}, "string"}, t]
> 
> Out[38]= {{Sin[t], 3 E, Cos[t]}, Tan[t], {Cos[t], Cos[Cos[t]], 1}, "string"}

The If will be faster if you swap its last two arguments
and don't complement the Or:

op[arr_, t_] := Map[If[NumericQ@# || StringQ@#, #, #@t]&, arr, {-1}]

[toc] | [standalone]


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


csiph-web