For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > August 2006 > How to handle Arrays that has functional parameters:









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to handle Arrays that has functional parameters:
Gopinath Venkatesan

2006-08-23, 8:04 am

Dear Friends

If we have to define an array but also work as a function, how do we define them?

For example, consider the following:

\!\(\(func1 = Table[a[i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
\(func2[s_, t_, i_, j_] := \(s\^i\) t\^j;\)\[IndentingNewLine]
\(Do[func1[\([i, j]\)] = func2[s, t,
i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
func1 // MatrixForm\)

Where I defined func1 to be an array, and func2 to be a function, and then assigned the func1 elements using func2 relation.

I wanted to know if we can build this relation directly into the array definition?

Like say, func1[[i,j]][s_,t_]:=s^i t^j, inside the Do loop, or other alternatives.

The reason is I have some functions of a variable, which I dont want to evaluate until the last step of the solution - i.e., I dont want to evaluate them during the do loop operations, and keep it safe and then evaluate in another do loop operation where
only the variables will change to evaluate the lagrangian and hence the individual solution for the variable value. I can use the following method, it takes some time to fix it, but would like to know if we can do it some other way.

Thanks,

Gopinath Venkatesan
Graduate Student
University of Oklahoma

Jens-Peer Kuska

2006-08-25, 4:05 am

Hi,

ff[i_, j_][s_, t_] := ff[i, j] = s^i*t^j

or

ffd[i_, j_][u_:s, v_:t] := ffd[i, j] = s^i*t^j

??

Regards

Jens

"Gopinath Venkatesan" <gopinathv@ou.edu> schrieb
im Newsbeitrag news:echej1$oqk$1@smc.vnet.net...
| Dear Friends
|
| If we have to define an array but also work as a
function, how do we define them?
|
| For example, consider the following:
|
| \!\(\(func1 = Table[a[i, j], {i, 3}, {j,
3}];\)\[IndentingNewLine]
| \(func2[s_, t_, i_, j_] := \(s\^i\)
t\^j;\)\[IndentingNewLine]
| \(Do[func1[\([i, j]\)] = func2[s, t,
| i, j], {i, 3}, {j,
3}];\)\[IndentingNewLine]
| func1 // MatrixForm\)
|
| Where I defined func1 to be an array, and func2
to be a function, and then assigned the func1
elements using func2 relation.
|
| I wanted to know if we can build this relation
directly into the array definition?
|
| Like say, func1[[i,j]][s_,t_]:=s^i t^j, inside
the Do loop, or other alternatives.
|
| The reason is I have some functions of a
variable, which I dont want to evaluate until the
last step of the solution - i.e., I dont want to
evaluate them during the do loop operations, and
keep it safe and then evaluate in another do loop
operation where only the variables will change to
evaluate the lagrangian and hence the individual
solution for the variable value. I can use the
following method, it takes some time to fix it,
but would like to know if we can do it some other
way.
|
| Thanks,
|
| Gopinath Venkatesan
| Graduate Student
| University of Oklahoma
|


Jean-Marc Gulliet

2006-08-25, 4:05 am

Gopinath Venkatesan wrote:
> If we have to define an array but also work as a function, how do we define them?
>
> For example, consider the following:
>
> \!\(\(func1 = Table[a[i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
> \(func2[s_, t_, i_, j_] := \(s\^i\) t\^j;\)\[IndentingNewLine]
> \(Do[func1[\([i, j]\)] = func2[s, t,
> i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
> func1 // MatrixForm\)
>
> Where I defined func1 to be an array, and func2 to be a function, and then assigned the func1 elements using func2 relation.
>
> I wanted to know if we can build this relation directly into the array definition?
>
> Like say, func1[[i,j]][s_,t_]:=s^i t^j, inside the Do loop, or other alternatives.
>
> The reason is I have some functions of a variable, which I dont want to evaluate until the last step of the solution - i.e., I dont want to evaluate them during the do loop operations, and keep it safe and then evaluate in another do loop operation wher

e only the variables will change to evaluate the lagrangian and hence the individual solution for the variable value. I can use the following method, it takes some time to fix it, but would like to know if we can do it some other way.

And using directly Table does not help?

func1= Table[ a[ i,j], { i,3}, { j,3}];
func2[ s_,t_,i_,j_]:=s^i*t^j;
Do[ func1[ [ i,j]]= func2[ s,t,i,j], { i,3}, { j,3}];

func3= Table[ func2[ s,t,i,j], { i,3}, { j,3}];
func3===func1

--> True

HTH,
Jean-Marc

Peter Pein

2006-08-25, 4:05 am

Gopinath Venkatesan schrieb:
> Dear Friends
>
> If we have to define an array but also work as a function, how do we define them?
>
> For example, consider the following:
>
> \!\(\(func1 = Table[a[i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
> \(func2[s_, t_, i_, j_] := \(s\^i\) t\^j;\)\[IndentingNewLine]
> \(Do[func1[\([i, j]\)] = func2[s, t,
> i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
> func1 // MatrixForm\)
>
> Where I defined func1 to be an array, and func2 to be a function, and then assigned the func1 elements using func2 relation.
>
> I wanted to know if we can build this relation directly into the array definition?
>
> Like say, func1[[i,j]][s_,t_]:=s^i t^j, inside the Do loop, or other alternatives.
>
> The reason is I have some functions of a variable, which I dont want to evaluate until the last step of the solution - i.e., I dont want to evaluate them during the do loop operations, and keep it safe and then evaluate in another do loop operation wher

e only the variables will change to evaluate the lagrangian and hence the individual solution for the variable value. I can use the following method, it takes some time to fix it, but would like to know if we can do it some other way.
>
> Thanks,
>
> Gopinath Venkatesan
> Graduate Student
> University of Oklahoma
>

Hi,

try Array:
funceasy = Array[s^#1t^#2 &, {3, 3}]



Bill Rowe

2006-08-25, 4:05 am

On 8/23/06 at 7:15 AM, gopinathv@ou.edu (Gopinath Venkatesan) wrote:

>If we have to define an array but also work as a function, how do we
>define them?


>For example, consider the following:
>
>\!\(\(func1 = Table[a[i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
>\(func2[s_, t_, i_, j_] := \(s\^i\) t\^j;\)\[IndentingNewLine]
>\(Do[func1[\([i, j]\)] = func2[s, t, i, j], {i, 3}, {j,
>3}];\)\[IndentingNewLine] func1 // MatrixForm\)


>Where I defined func1 to be an array, and func2 to be a function,
>and then assigned the func1 elements using func2 relation.


When you do:

func1 = Table[a[i, j], {i, 3}, {j, 3}];

you are defining func1 to be an array where each element is the function a evaluated at the array indices. Is this what you intended?

Once you have defined func1 it seems you want now define function a to be as you have defined func2. If I have this correct, it seems simple pattern matching would be effective here, i.e.,

In[14]:=
(#1 /. a[i_, j_] :> s^i*t^j & ) //@ func1

Out[14]=
{{s*t, s*t^2, s*t^3}, {s^2*t, s^2*t^2, s^2*t^3},
{s^3*t, s^3*t^2, s^3*t^3}}

--
To reply via email subtract one hundred and four

Gopinath Venkatesan

2006-08-26, 4:02 am

My sincere Thanks to Jean, Daniel, and Peter for suggesting me different methods for doing the same operation.

I followed Daniel's suggestion as I wanted to attach the functional parameters to the array assignment itself.

So if I had to extract the array value at specific [i, j] position, and for a given value of s and t, it is easy to do so.

Gopinath
Graduate Student
University of Oklahoma

Norbert Marxer

2006-08-26, 4:02 am

Hello

If you set

func1[s_, t_] = Table[s^i t^j, {i, 3}, {j, 3}]

the command

func1[3, 4][[2, 3]]

will return the number 576, which is the element in the 2nd row and the
3rd column of your array and where you used s=3 and t=4.

I hope this is what you want.

Best Regards
Norbert Marxer
www.mec.li


Gopinath Venkatesan wrote:
> Dear Friends
>
> If we have to define an array but also work as a function, how do we define them?
>
> For example, consider the following:
>
> \!\(\(func1 = Table[a[i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
> \(func2[s_, t_, i_, j_] := \(s\^i\) t\^j;\)\[IndentingNewLine]
> \(Do[func1[\([i, j]\)] = func2[s, t,
> i, j], {i, 3}, {j, 3}];\)\[IndentingNewLine]
> func1 // MatrixForm\)
>
> Where I defined func1 to be an array, and func2 to be a function, and then assigned the func1 elements using func2 relation.
>
> I wanted to know if we can build this relation directly into the array definition?
>
> Like say, func1[[i,j]][s_,t_]:=s^i t^j, inside the Do loop, or other alternatives.
>
> The reason is I have some functions of a variable, which I dont want to evaluate until the last step of the solution - i.e., I dont want to evaluate them during the do loop operations, and keep it safe and then evaluate in another do loop operation wher

e only the variables will change to evaluate the lagrangian and hence the individual solution for the variable value. I can use the following method, it takes some time to fix it, but would like to know if we can do it some other way.
>
> Thanks,
>
> Gopinath Venkatesan
> Graduate Student
> University of Oklahoma


Gopinath Venkatesan

2006-08-26, 4:02 am

Thanks Jens and Bill.

I missed your replies. I used to receive individual mails for the question posted but somehow didnt receive from you.

Gopinath

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com