Message Boards Message Boards

0
|
7918 Views
|
7 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Function parameters - without evaluation.

Posted 10 years ago

I have some sample code:

constantmatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
f[a_, b_, constant_] := a + b + Plus @@ Flatten[constant]

I then want to enter some parameters in to the function. The following works:

list = {#, # + 1, constantmatrix} & /@ Range[5]
Apply[f , list, 1]

However I want list to just show "constantmatrix" rather than the matrix values. So the following code does not work, but is in the spirit of what i want to do:

list = {#, # + 1, "constantmatrix"} & /@ Range[5]
Apply[f , list, 1]

Although the "list" looks okay, since it's a String - i can't get it in to the function. Any help on this would be appreciated!

{{1, 2, "constantmatrix"}, {2, 3, "constantmatrix"}, {3, 4, 
  "constantmatrix"}, {4, 5, "constantmatrix"}, {5, 6, 
  "constantmatrix"}}
POSTED BY: Priyan Fernando
7 Replies

Let me see if I understand. The result of

list = {#, # + 1, "constantmatrix"} & /@ Range[5]

must be

{{1, 2, "constantmatrix"}, {2, 3, "constantmatrix"}, {3, 4, 
  "constantmatrix"}, {4, 5, "constantmatrix"}, {5, 6, 
  "constantmatrix"}}

and the final result of

Apply[f, list, 1]

must be

{48, 50, 52, 54, 56}

It would be much better for us if you could explain why you need that string in the intermediate result. Is this only a challenge?

Clear constantmatrix and enter it without the quotation marks.

POSTED BY: Frank Kampas

Try HoldForm[constantmatrix]

POSTED BY: Frank Kampas
Posted 10 years ago

Hi Frank,

Sure I can do that, but then I can't apply the function as constantmatrix is not defined. Maybe I wasn't clear about the code I need. I want the list to show "constantmatrix" unevaluated (because in reality constant matrix will be a large matrix and list will be really long), yet for the function to work:

In[7]:= Clear[constantmatrix]
list = {#, # + 1, constantmatrix} & /@ Range[5]

Out[8]= {{1, 2, constantmatrix}, {2, 3, constantmatrix}, {3, 4, 
  constantmatrix}, {4, 5, constantmatrix}, {5, 6, constantmatrix}}

This is fine. Now if I define constant matrix and examine list:

constantmatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9};

In[10]:= list

Out[10]= {{1, 2, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}, {2, 
  3, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}, {3, 
  4, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}, {4, 
  5, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}, {5, 
  6, {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}}}

Then I have the constantmatrix repeated in list - I want to avoid this - because the actual constantmatrix is very large and list size (which come from Range[x] is also very long). Hence list might blow up which I want to avoid.

POSTED BY: Priyan Fernando
Posted 10 years ago

Still struggling with this Frank! If you look at the code below:

 In[30]:= Clear[constantmatrix]
    list = {#, # + 1, constantmatrix} & /@ Range[100];
    ByteCount[list]
    constantmatrix = RandomReal[1000, {100, 100}];
    ByteCount[constantmatrix]
    ByteCount[list]

Out[32]= 10496

Out[34]= 80152

Out[35]= 8025696

Here list blows up in size as soon as I define constant matrix - because the same matrix is repeated in list. However knowing that this is the same list, is there an efficient way to define the function (or some other way) - which utilizes the information that constantmatrix will be the same for every evaluation of the function:

f[a_, b_, constant_] := a + b + Plus @@ Flatten[constant]
Apply[f, list, 1]

Sorry if I am being unclear.

POSTED BY: Priyan Fernando

I have to admit I don't understand what you are trying to do. I can see that you don't want 100 copies of your constant matrix in the list, but I don't know why you'd want to do that in the first place.

POSTED BY: Frank Kampas
Posted 10 years ago

Thanks for the comments. This was not a challenge, and I guess I wasn't clear on the code. However, I resolved the issue by making constantmatrix a global variable. Thanks for the help.

POSTED BY: Priyan Fernando
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract