Message Boards Message Boards

0
|
8081 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Define a function of many variables

Posted 4 years ago

The basic idea is that I want to define a function whose number of variables depends on the size of an imported spreadsheet. This might be thousands of variables. While working on an optimization problem, I encountered this problem: I need to define a function of many variables.

I naively try to define f as a function of 100 variables as follows:

f[ Table[ x[n]_, {n,1,100} ] ] = Sum[ x[n], {n,1,100} ]

I'm just making up an example. I would like to define something like x[1], x[2], where x[n] is the nth variable.

How do I define a function with an unknown number of many variables?

TIA

POSTED BY: Brian Tenneson
3 Replies
Anonymous User
Anonymous User
Posted 4 years ago

Assume Import[] gave you a list x of values or expressions. You may write:

f[w_] := ...

and give x to f, then inside your function you may use

Length@w
w[[i]]

If you need it's length or ith value. But you usually will use x[[i]] if you are altering the order or limiting in which the elements are used. say, h[x[[2]], x[[1]], x[[3]]], an ad-hoc re-ordering of the list. The other way to do that is with Array rather than list (used in certain multi-dimensional uses, List is usually easier to deal with)

You may write:

f := Plus@@x

and use f without ever defining arguments or just Plus@@x, problem solved, no need for function.

Avoid using "same names" (x inside your function same name as outside your function) until you've become familiar with Context[] and how variables are named inside and outside functions.

  f[x_] used as f[x]

can give you headaches (since you'll learn about Rule soon and use it - and get confused when it sometimes gets confusing inside functions returning symbols) ... so

 f[w_] and f[x]

is wiser for now because the names are unique and won't be confused. Infact, if your function will return symbols - you might consider not using "local variables" until you learn more about Symbol[], Context[], Head[], and many other matters in The Mathematica Book introduces to new users.

The Mathematica Book (as opposed to Help) is a great resourse, I suggest reading at least the first half of it.

POSTED BY: Anonymous User
Anonymous User
Anonymous User
Posted 4 years ago

Your variables can be kept in a List[]. X={a,b,c,d,...}. There is no reason to make them "arguments to a function" like you'd do using "a programming language". The list can be (Evalutate[]'ed) as expressions inside any function or without any function at all.

X=Get["file.part"]
(* will read file as mathematica expressions, evaluated upon reading *)

If file contains: List[ {expr1, expr2, expr3}, then X is a List of variables, Plus@@X adds them all without using Part[], meaning X[[1]]+X[[2]+X[[3]].

If file contains "expr1,expr2,expr3" I might write, to satisfy expression syntax,

X=List[Get["file.part"]]

Function arguments, in Mathematica, can act like a FILTER, calling a correct function on the basis of the PATTERN of arguments given or even altering the pattern of the arguments to fit a function argument. (this would protect a function from giving a result on spurious or incomplete arguments, and from errors that might cause the Kernel to Quit[]). Functions act as an expression that expect parameters - so if your math expects to give arguments you might use a function to take them (on the other hand, mathematica allows you to convert arguments, which are a Sequence[], to type List, so you are not forced to use a function). Also, functions do not "need to be defined" like a programming language. this un-named "dis-appearing function" takes argument 1 and does it job without ever being named.

(1+#)&[1] == 2

But it doesn't really dis-appear. All your Input[] and Output is remembered! You can change the length of how many Input[] of the past Mathematica remembers.

I am just discouraging "making a function that takes the variables". A List should "take the variables". A function will not "hold them" (it doesn't in lower level languages like C either).

f[x__] := With[{args = {x}}, Sum[args[[i]], {i, Length@args}]]

The problem here x is an unkown Sequence. (we didn't use f[x_List] wich would certainly be a simple list). So then needlessly you've asked another function Which to count AND evaluate btw all of the sequences one at a time (which is what you want to avoid ALWAYS) to create {x} and args (args becomes a list), and use X[[i]] in all your computations. If x expands strangely, you could get in allot of trouble. Another problem is you were not told how to "get x into f as a bound sequence object", which you might have been wondering.

So I can't agree that f[x__] used with With[ ..., i, Lenght@foo] is what you are looking for. It is the opposite of what would be "simplest" and also most efficient.

POSTED BY: Anonymous User
Posted 4 years ago
f[x__] := With[{args = {x}}, Sum[args[[i]], {i, Length@args}]]

f[1, 2, 3, 4, 5]
(* 15 *)
POSTED BY: Hans Milton
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