Re: Matrices as operators
- To: mathgroup at smc.vnet.net
- Subject: [mg123225] Re: Matrices as operators
- From: Ray Koopman <koopman at sfu.ca>
- Date: Sun, 27 Nov 2011 04:14:07 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
----- Christopher Young <cy56 at 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}]