Let’s walk through a simpler example, but starting with something analogous to your function. (I can’t see all of your code, btw. You should follow the guidance from the moderation team about how to paste code.)
In[]:=
Clear[myArray,meijres]
In[]:=
meijres[x_,y_,z_,u_]=​​Function[​​{k,theta,mu,c},​​If[mu==1,myArray=ConstantArray[1,31]];​​If[mu==2,myArray=ConstantArray[2,31]];​​If[mu==3,myArray=ConstantArray[.5,31]];​​Sum[SpecialFunction[myArray[[t]]],{t,1,31}]][x,y,z,u]
Part
:Part specification myArray〚1〛 is longer than depth of object.
Part
:Part specification myArray〚2〛 is longer than depth of object.
Part
:Part specification myArray〚3〛 is longer than depth of object.
General
:Further output of Part::partd will be suppressed during this calculation.
Out[]=
SpecialFunction[myArray〚1〛]+SpecialFunction[myArray〚2〛]+SpecialFunction[myArray〚3〛]+SpecialFunction[myArray〚4〛]+SpecialFunction[myArray〚5〛]+SpecialFunction[myArray〚6〛]+SpecialFunction[myArray〚7〛]+SpecialFunction[myArray〚8〛]+SpecialFunction[myArray〚9〛]+SpecialFunction[myArray〚10〛]+SpecialFunction[myArray〚11〛]+SpecialFunction[myArray〚12〛]+SpecialFunction[myArray〚13〛]+SpecialFunction[myArray〚14〛]+SpecialFunction[myArray〚15〛]+SpecialFunction[myArray〚16〛]+SpecialFunction[myArray〚17〛]+SpecialFunction[myArray〚18〛]+SpecialFunction[myArray〚19〛]+SpecialFunction[myArray〚20〛]+SpecialFunction[myArray〚21〛]+SpecialFunction[myArray〚22〛]+SpecialFunction[myArray〚23〛]+SpecialFunction[myArray〚24〛]+SpecialFunction[myArray〚25〛]+SpecialFunction[myArray〚26〛]+SpecialFunction[myArray〚27〛]+SpecialFunction[myArray〚28〛]+SpecialFunction[myArray〚29〛]+SpecialFunction[myArray〚30〛]+SpecialFunction[myArray〚31〛]
What happened? Several things, unfortunately.
One thing is that you’re using a simple Set (“=”), so none of the evaluations are being delayed. You’re basically trying to define a static value for the expression meijres[x_,y_,z_,u_] by applying an anonymous function to the four inputs x, y, z, u. None of the If conditions apply, so when it tries to evaluate the Sum expression, the symbol myArray has no value assigned to it. Thus, Part (“[[...]]”) cannot successfully extract anything. Part is designed to print out an error message in that case.
We can actually see what Mathematica knows about at this point:
In[]:=
??myArray
Out[]=
Symbol
Global`myArray
Full Name
Global`myArray
​
In[]:=
??meijres
Out[]=
Symbol
Global`meijres
Definitions
meijres[x_,y_,z_,u_]=SpecialFunction[myArray〚1〛]+SpecialFunction[myArray〚2〛]+SpecialFunction[myArray〚3〛]+SpecialFunction[myArray〚4〛]+SpecialFunction[myArray〚5〛]+SpecialFunction[myArray〚6〛]+SpecialFunction[myArray〚7〛]+SpecialFunction[myArray〚8〛]+SpecialFunction[myArray〚9〛]+SpecialFunction[myArray〚10〛]+SpecialFunction[myArray〚11〛]+SpecialFunction[myArray〚12〛]+SpecialFunction[myArray〚13〛]+SpecialFunction[myArray〚14〛]+SpecialFunction[myArray〚15〛]+SpecialFunction[myArray〚16〛]+SpecialFunction[myArray〚17〛]+SpecialFunction[myArray〚18〛]+SpecialFunction[myArray〚19〛]+SpecialFunction[myArray〚20〛]+SpecialFunction[myArray〚21〛]+SpecialFunction[myArray〚22〛]+SpecialFunction[myArray〚23〛]+SpecialFunction[myArray〚24〛]+SpecialFunction[myArray〚25〛]+SpecialFunction[myArray〚26〛]+SpecialFunction[myArray〚27〛]+SpecialFunction[myArray〚28〛]+SpecialFunction[myArray〚29〛]+SpecialFunction[myArray〚30〛]+SpecialFunction[myArray〚31〛]
Full Name
Global`meijres
​
One thing to note is that myArray is in the Global namespace. That’s probably not what you want. You probably want it to be local to your function definition. But we’ll get back to that later.
What if we give myArray a value?
In[]:=
myArray=ConstantArray[0,31]
Out[]=
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
And let’s re-evaluate your expression defining meijres (you could just evaluate the cell near the top, but I’ll duplicate it here for convenience).
In[]:=
meijres[x_,y_,z_,u_]=​​Function[​​{k,theta,mu,c},​​If[mu==1,myArray=ConstantArray[1,31]];​​If[mu==2,myArray=ConstantArray[2,31]];​​If[mu==3,myArray=ConstantArray[.5,31]];​​Sum[SpecialFunction[myArray[[t]]],{t,1,31}]][x,y,z,u]
Out[]=
31SpecialFunction[0]
Okay, now we can access parts of myArray, and the Sum can reduce to something.
But, none of this is what you’re trying to do. So, we need to back up and cover some Mathematica basics.
At an introductory level, you can think of square brackets (single, not double) as a way to pass arguments to a function. So, the expression Abc[1,2,3] can represent a function (that we are calling “Abc”) to which we are passing three arguments. The built-in function called “Function” represents an anonymous function, and a Function expression will “operate” on an argument list if it is given one. So, consider these examples:
In[]:=
Function[{a,b,c},{a,b,c,c,b,a}]
Out[]=
Function[{a,b,c},{a,b,c,c,b,a}]
In[]:=
Function[{a,b,c},{a,b,c,c,b,a}][1]
Function
:Too many parameters in {a,b,c} to be filled from Function[{a,b,c},{a,b,c,c,b,a}][1].
Out[]=
Function[{a,b,c},{a,b,c,c,b,a}][1]
In[]:=
Function[{a,b,c},{a,b,c,c,b,a}][1,2,3]
Out[]=
{1,2,3,3,2,1}
In[]:=
Function[{a,b,c},{a,b,c,c,b,a}][1,2,3,4]
Out[]=
{1,2,3,3,2,1}
You can assign any expression to a symbol, including a Function expression. Consider this...
In[]:=
myNewFunc=Function[{a},a^3]
Out[]=
Function[{a},
3
a
]
In[]:=
myNewFunc[4]
Out[]=
64
This is all leading to our first improvement. If you wanted to define meijres as a Function, then you don’t need the formal argument list [x_,y_,z_,u_]. You could just do this:
In[]:=
Clear[meijres,myArray]
In[]:=
meijres=​​Function[​​{k,theta,mu,c},​​If[mu==1,myArray=ConstantArray[1,31]];​​If[mu==2,myArray=ConstantArray[2,31]];​​If[mu==3,myArray=ConstantArray[.5,31]];​​Sum[SpecialFunction[myArray[[t]]],{t,1,31}]]
Out[]=
Function{k,theta,mu,c},If[mu1,myArray=ConstantArray[1,31]];If[mu2,myArray=ConstantArray[2,31]];If[mu3,myArray=ConstantArray[0.5,31]];
31
∑
t=1
SpecialFunction[myArray〚t〛]
Let’s try it out:
In[]:=
meijres[x,y,z,u]
Part
:Part specification myArray〚1〛 is longer than depth of object.
Part
:Part specification myArray〚2〛 is longer than depth of object.
Part
:Part specification myArray〚3〛 is longer than depth of object.
General
:Further output of Part::partd will be suppressed during this calculation.
Out[]=
SpecialFunction[myArray〚1〛]+SpecialFunction[myArray〚2〛]+SpecialFunction[myArray〚3〛]+SpecialFunction[myArray〚4〛]+SpecialFunction[myArray〚5〛]+SpecialFunction[myArray〚6〛]+SpecialFunction[myArray〚7〛]+SpecialFunction[myArray〚8〛]+SpecialFunction[myArray〚9〛]+SpecialFunction[myArray〚10〛]+SpecialFunction[myArray〚11〛]+SpecialFunction[myArray〚12〛]+SpecialFunction[myArray〚13〛]+SpecialFunction[myArray〚14〛]+SpecialFunction[myArray〚15〛]+SpecialFunction[myArray〚16〛]+SpecialFunction[myArray〚17〛]+SpecialFunction[myArray〚18〛]+SpecialFunction[myArray〚19〛]+SpecialFunction[myArray〚20〛]+SpecialFunction[myArray〚21〛]+SpecialFunction[myArray〚22〛]+SpecialFunction[myArray〚23〛]+SpecialFunction[myArray〚24〛]+SpecialFunction[myArray〚25〛]+SpecialFunction[myArray〚26〛]+SpecialFunction[myArray〚27〛]+SpecialFunction[myArray〚28〛]+SpecialFunction[myArray〚29〛]+SpecialFunction[myArray〚30〛]+SpecialFunction[myArray〚31〛]
One important thing to notice here is that we didn’t get the error message until after we applied the function. That’s because Function holds its arguments unevaluated.
Let’s try something that will trigger one of the If conditions:
In[]:=
meijres[x,y,1,u]
Out[]=
31SpecialFunction[1]
Unfortunately, this had a side effect
In[]:=
??myArray
Out[]=
Symbol
Global`myArray
Assignment
myArray={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
Full Name
Global`myArray
​
Which means ...
Okay, let’s deal with the default condition. Since you have several cases, Which or Switch would probably be better than a sequence of If expressions.
Bingo!
Okay, but this is still a mess. We really don’t want myArray to be in the Global namespace, and this just feels very awkward. We can “localize” the helper variable myArray like this:
The variable myArray has no value associated to it, so there is no side effect.
Now, there are very frequently times when you want to assign a Function expression to a variable, but in your case, I suspect that you want to use SetDelayed instead:
We can take things even further, but I think we should pause and let you digest that and ask any follow up questions.