MuPAD evaluation of local variables, Sum of row of array - matlab

I have discovered a strange behavior in MuPAD version 5.7.0 (MATLAB R2011b), and I would like to know whether this is a bug, and if not so, what I am doing wrong. Ideally, I would also like to know why MuPAD does what it does.
Consider the array C of size 3x3, the elements of which have some example values. I would like to regard this array as an array of arrays and thus use cascaded indexing.
The problem apparently arises when both indices are local variables of different nested scopes, namely when the first index's scope is wider than the second index's one. There is no problem if the first index is a constant.
When I enter:
reset();
C := [[a,b,c],[d,e,f],[g,h,i]];
sum((C[3])[t], t = 1..3);
S := j -> sum((C[j])[t], t = 1..3);
S(3);
I get the following result:
I would expect lines 3 and 5 in the code (2 and 4 in the output) to yield the same result: g+h+i. Instead, line 5 produces a+e+i, which seems to be the diagonal of C.
When I do the same with product instead of sum, the result is even stranger, but might reveal more about the "error's" source, particularly DOM_VAR(0,2):
reset();
C := [[a,b,c],[d,e,f],[g,h,i]];
product((C[3])[t], t = 1..3);
eval(product((C[3])[t], t = 1..3));
S := j -> product((C[j])[t], t = 1..3);
S(3);
eval(S(3));
I get:
I might be on the wrong track here, but I suspect that a closure is created that tried to save the surrounding scope's local variables, which are undetermined at the time of the closure's creation. Also, substitutions seem to stop at some point, which is overridden by eval().
Practical Problem
The practical problem I am trying to solve is the following:
reset();
aVec := Symbol::accentUnderBar(Symbol::alpha)
Problem: Calculate multinomials of the form
hold(sum(x(i), i=i_0..i_k)^n)
On Wikipedia, the following form is defined:
sumf := freeze(sum):
hold(sum(x[i], i=1..m)^n)=sumf(binomial(n,aVec)*product(x[t]^aVec[t], t = 1..m), abs(aVec)=n);
In order to implement this, we need to define the set of vectors alpha, the sum of which equals m.
These correspond to the set of possible compositions of n with length m and possible zero elements:
C := (n,m) -> combinat::compositions(n, MinPart = 0, Length = m)
For example, the sum
n := 3:
m := 3:
sumf(x[i], i=1..m)^n = sum(x[i], i=1..m)^n;
would call for these combinations of powers, each of which is one vector alpha:
A := C(n,m)
Additionally, we need the multinomial coefficients.
Each such coefficient depends on vector alpha and the power n:
multinomial := (n, aVec) -> fact(n) / product(fact(aVec[k]), k = 1..nops(aVec))
For example, the number of times that the second composition appears, is:
multinomial(n, A[2])
Summation over all compositions yields:
sum(multinomial(n,A[i])*product(x[t]^A[i][t], t = 1..m), i = 1..nops(A))
The powers are correct, but the coefficients are not. This seems related to the boiled-down abstract problem stated first in this question.

The expression [[a,b,c],[d,e,f],[g,h,i]] is not an array in MuPAD. It is a "list of lists." I'm guessing that's not what you're after. Lists are commonly used to initialize arrays and matrices and other objects (more here). For these examples, either arrays or matrices will work, but note that these two data types have different advantages.
Using array:
reset();
C := array([[a,b,c],[d,e,f],[g,h,i]]);
sum(C[3, t],t=1..3);
S := j -> sum(C[j, t], t = 1..3);
S(3);
which returns
Note the different way row/column indexing is represented relative to that in your question. Similarly, modifying your other example
reset();
C := matrix([[a,b,c],[d,e,f],[g,h,i]]);
product(C[3, t], t = 1..3);
S := j -> product(C[j, t], t = 1..3);
S(3);
results in
If you do happen to want to use lists for this, you can do so like this
reset();
C := [[a,b,c],[d,e,f],[g,h,i]];
_plus(op(C[3]));
S := j -> _plus(op(C[j]));
S(3);
which returns
The _plus is the functional form of + and op extracts each element from the list. There are other ways to do this, but this is one of the simplest.

I am posting this answer only to provide a working example of the practical problem from the question. horchler provided the decisive solution by proposing to use a matrix instead of a list of lists.
Basically, this is a modified transscript of the practical problem.
Practical Solution
The practical problem I am trying to solve is the following:
reset();
aVec := Symbol::accentUnderBar(Symbol::alpha)
Problem: Calculate multinomials of the form
hold(sum(x(i), i=i_0..i_k)^n)
On Wikipedia, the following form is defined:
sumf := freeze(sum):
hold(sum(x[i], i=1..m)^n)=sumf(binomial(n,aVec)*product(x[t]^aVec[t], t = 1..m), abs(aVec)=n);
In order to implement this, we need to define the set of vectors alpha, the sum of which equals m.
These correspond to the set of possible compositions of n with length m and possible zero elements:
C := (n,m) -> combinat::compositions(n, MinPart = 0, Length = m)
For example, the sum
n := 3:
m := 3:
sumf(x[i], i=1..m)^n = sum(x[i], i=1..m)^n;
would call for these combinations of powers, each of which is one vector alpha:
A := matrix(nops(C(n,m)),m,C(n,m));
Additionally, we need the multinomial coefficients.
Each such coefficient depends on vector alpha and the power n:
multinomial := (n, aVec) -> fact(n) / product(fact(aVec[k]), k = 1..nops(aVec))
For example, the number of times that the second composition appears, is:
multinomial(n,A[2,1..m])
Summation over all compositions yields:
sum(multinomial(n,A[i,1..m])*product(x[t]^A[i,t], t = 1..m), i = 1..nops(C(n,m)));
Finally, prove that the result transforms back:
simplify(%)

Related

hyperpolic function integration in maple

Im trying to evaluate this function in maple
but I keep getting this answer, why isn't maple integrating properly. I tried numerically integrating it and it works but I need the analytical solution too.
restart;
sig := x->(exp((x-t)/a)-exp((-x-t)/a))
/(exp((x-t)/a)+exp((-x-t)/a)):
new := convert(simplify(convert(expand(sig(x)),trigh)),tanh);
new := tanh(x/a)
simplify(expand(convert(sig(x) - new, exp)));
0
Now, you originally wrote int(f*sig(x)/x,x).
You didn't indicate that f was a function of x, and as a mere constant it's not really important and could simply be pulled out in front of the integral as a constant factor. If f is some function of x then you really need to state what it is!
Let's consider int(sig(x)/x,x=c..d). Using the simplification new, that is just,
Q := Int( new/x, x=c..d );
Q := Int(tanh(x/a)/x, x = c .. d)
QQ := IntegrationTools:-Change(Q, y=x/a, y);
QQ := Int(tanh(y)/y, y = c/a .. d/a)
You said that you wanted an "analytical solution" by which I take it you mean an explicit formula for the symbolic integration result. But what do you want if the integral does not (mathematically) have a closed form exact, symbolic result?
Would you be content with an (exact, symbolic) series approximation?
H := (a,ord,c,d)
-> int(convert(series(eval(new/x,:-a=a),x,ord),
polynom),x=c..d):
# order 5
H(a, 5, c, d);
3 3 / 5 5\
d - c -c + d 2 \-c + d /
----- - -------- + ------------
a 3 5
9 a 75 a
For a specific example, taking a=2 and an (exact) series approximation of order 25, then the integral from x=0 to x=1 gets evaluated as an exact rational.
evalf(H(2, 25, 0, 1));
0.4868885956
Here's the numeric integration for those same values,
evalf(Int( eval(new/x,a=2), x=0..1 ));
0.4868885956
Specialized numeric quadrature could be as good as a series approximation for a variety of applications, but of course that would depend on what you intend on doing with the result.
This raises the question: what do you hope to do with some supposed "analytical result" that you cannot do with a black-box function that generates the floating-point numeric approximation? Why do you "need" an "analytic result"?
BTW, another way to simplify it (in case the construction above of new does not succeed in your Maple version):
new := convert(simplify(expand( numer(sig(x))/exp(-t/a) ))
/simplify(expand( denom(sig(x))/exp(-t/a) )),
compose,trigh,tanh);
/x\
new := tanh|-|
\a/

rewrite MATLAB to Maple

I have a small MATLAB symbolic script as following
syms z;
psi(2)=exp(2*z-exp(z))/(1-exp(-exp(z)));
psi(3)=exp(2*z-exp(z))/(1-exp(-exp(z)))*z;
psi(4)=exp(2*z-exp(z))/(1-exp(-exp(z)))*z^2;
f(1,1)=exp(2*z-exp(z))/(1-exp(-exp(z)));
for i=2:4
f(i,1)=diff(psi(i),z);
for j=2:i
f(i,j)=diff(f(i,j-1)/f(j-1,j-1),z);
end
end
given a symbolic vector psi consist of functions of z, it create a lower triangle symbolic matrix f. it works well.
I'm trying to rewrite this part in Maple, which I'm new to. I tried
psi(2) := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi(3) := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi(4) := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f(1, 1) := exp(2*z-exp(z))/(1-exp(-exp(z)));
for i from 2 to 4 do f(i,1):=exp(2*z-exp(z))/(1-exp(-exp(z)));
for j from 2 to i do f(i,j):=diff(f(i,j-1)/f(j-1,j-1),z);
od;
od;
something ambiguous in the "diff" line, I just select function definition. if I let it output f(4,4), it report
Error, (in f) too many levels of recursion
but it did print f(4,1).
could some one tell what's wrong? Thanks!
Your code is pretty close (and reminds me how similar these two languages are at times). The reason for the error message is that you need to declare f before you start filling it with values.
Here's one possible solution:
psi[2] := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi[3] := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi[4] := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f := Matrix(1..4,1..4):
f[1, 1] := exp(2*z-exp(z))/(1-exp(-exp(z))):
for i from 2 to 4 do
f[i,1] := diff(psi[i],z):
for j from 2 to i do
f[i,j] := diff(f[i,j-1]/f[j-1,j-1],z):
end do:
end do:
f;
Note here that I declare f to be a 4x4 Matrix before I start filling it. Also, here the [] notation is used for specifying indices.
Another option which may scale better for larger problems is to grow your data structure for f as you add values to it. Here we start with a 1x1 Array and add values to it.
psi[2] := exp(2*z-exp(z))/(1-exp(-exp(z)));
psi[3] := exp(2*z-exp(z))*z/(1-exp(-exp(z)));
psi[4] := exp(2*z-exp(z))*z^2/(1-exp(-exp(z)));
f:=Array(1..1,1..1):
f(1, 1) := exp(2*z-exp(z))/(1-exp(-exp(z))):
for i from 2 to 4 do
f(i,1):=diff(psi[i],z):
for j from 2 to i do
f(i,j):=diff(f[i,j-1]/f[j-1,j-1],z):
end do:
end do:
f;
Here you'll notice that we are using the () notation for Array indices at time of creation. If you use an Array for storage, this is one technique that allows for you to grow the Array as you add values.
Now in both cases you can also note that I've used [] to index a term that already exists; square brackets are the default notation in Maple for specifying indices in a data structure.
Also note that I've suppressed output in each loop using the : operator; this way you can just echo back the resulting Matrix f at the end.

Custom correlation function in matlab

I am trying to create a custom function for pearson's correlation coefficient with this code in matlab 2010
function [p] = customcorr(o)
x := a
y := b
x_mean := mean(a)
y_mean := mean(b)
x_std := std(a)
y_std := std(b)
n := length(o)
r := (1/(n-1))*((x-x_mean)*(y-y_mean))/(x_std*y_std)
end
But i get an error when trying to execute it
Error in ==> customcorr at 2
x := a
Anybody might know what the problem is? Thank you
First, check the correct MATLAB syntax: a "normal" assignment is done by =, not by :=.
Second, you use a and b but these are not defined as parameters of the function. Replace the function head by function p = customcorr(a,b).
Third, I am not really sure what o should be, I assume it can be replaced by length(a) or length(b).
The estimator for an unbiased correlation coefficient is given by
(from wikipedia)
Thus you need to sum all the (a-a_mean).*(b-b_mean) up with sum. Note that it is required to write .* to get the element-wise multiplication. That way you subtract the mean from each element of the vectors, then multiply the corresponding a's and b's and sum up the results of these multiplications.
Together this is
function p = customcorr(a,b)
a_mean = mean(a);
b_mean = mean(b);
a_std = std(a);
b_std = std(b);
n = length(a);
p = (1/(n-1)) * sum((a-a_mean).*(b-b_mean)) / (a_std*b_std);
end
What MATLAB does in their corr function (besides many other interesting things) is, they check the number of arguments (nargin variable) to see if a and b were supplied or not. You can do that by adding the following code to the function (at the beginning)
if nargin < 2
b = a;
end

How to provide the list of diff between 2 lists

I have 2 cells
exp = {'test','tat','toto'};
act = {'test','toto','tat','pto'};
and would like to check these lists are equal. How could I provide the list of differences when they don't have the same number of elements?
With setdiff the order of the input matters. setdiff(A,B) returns a list of entries in A which are not in B only, it does not return those entries in B which are not in A.
exp = {'test','tat','toto'};
act = {'test','toto','tat','pto'}
setdiff(exp,act); % empty because there is nothing in exp which isn't in act
setdiff(act,exp); %returns 1 x 1 cell, 'pto'.
Instead, use setxor(A,B), which returns values not in the intersection of A and B. Order of inputs doesn't matter:
exp = {'test','tat','toto','pta'};
act = {'test','toto','tat','pto'};
setxor(exp,act) % returns 'pta','pto'
You can use the setdiff command.
exp = {'test', 'tat', 'toto'};
act = {'test', 'toto', 'tat', 'pto'};
diff = setdiff(exp, act);
You can find it in the documentation:
http://www.mathworks.com/help/matlab/ref/setdiff.html?refresh=true
In short, usesetdiff; see the documentation, there is an example on exactly what you want to do.
EDIT
Explanation is needed about setdiff. According to the documentation:
C = setdiff(A,B) returns the data in A that is not in B.
This sentence must be understood exactly: it returns the data of A not in B. So setdiff is NOT symmetric with its arguments! For your problem, if all elements of A are in B, even if B is bigger, the result set is empty.
To get the differences between two sets, in other words you want a symmetric function wrt their arguments, Matlab offers another function, setxor:
C = setxor(A,B) returns the data of A and B that are not in their intersection (the symmetric difference).

Symbolic integration vs numeric integration in MATLAB

I have an expression with three variables x,y and v. I want to first integrate over v, and so I use int function in MATLAB.
The command that I use is the following:
g =int((1-fxyz)*pv, v, y,+inf)%
PS I haven't given you what the function fxyv is but it is very complicated and so int is taking so long and I am afraid after waiting it might not solve it.
I know one option for me is to integrate numerically using for example integrate, however I want to note that the second part of this problem requires me to integrate exp[g(x,y)] over x and y from 0 to infinity and from x to infinity respectively. So I can't take numerical values of x and y when I want to integrate over v I think or maybe not ?
Thanks
Since the question does not contain sufficient detail to attempt analytic integration, this answer focuses on numeric integration.
It is possible to solve these equations numerically. However, because of complex dependencies between the three integrals, it is not possible to simply use integral3. Instead, one has to define functions that compute parts of the expressions using a simple integral, and are themselves fed into other calls of integral. Whether this approach leads to useful results in terms of computation time and precision cannot be answered generally, but depends on the concrete choice of the functions f and p. Fiddling around with precision parameters to the different calls of integral may be necessary.
I assume that the functions f(x, y, v) and p(v) are defined in the form of Matlab functions:
function val = f(x, y, v)
val = ...
end
function val = p(v)
val = ...
end
Because of the way they are used later, they have to accept multiple values for v in parallel (as an array) and return as many function values (again as an array, of the same size). x and y can be assumed to always be scalars. A simple example implementation would be val = ones(size(v)) in both cases.
First, let's define a Matlab function g that implements the first equation:
function val = g(x, y)
val = integral(#gIntegrand, y, inf);
function val = gIntegrand(v)
% output must be of the same dimensions as parameter v
val = (1 - f(x, y, v)) .* p(v);
end
end
The nested function gIntegrand defines the object of integration, the outer performs the numeric integration that gives the value of g(x, y). Integration is over v, parameters x and y are shared between the outer and the nested function. gIntegrand is written in such a way that it deals with multiple values of v in the form of arrays, provided f and p do so already.
Next, we define the integrand of the outer integral in the second equation. To do so, we need to compute the inner integral, and therefore also have a function for the integrand of the inner integral:
function val = TIntegrandOuter(x)
val = nan(size(x));
for i = 1 : numel(x)
val(i) = integral(#TIntegrandInner, x(i), inf);
end
function val = TIntegrandInner(y)
val = nan(size(y));
for j = 1 : numel(y)
val(j) = exp(g(x(i), y(j)));
end
end
end
Because both function are meant to be fed as an argument into integral, they need to be able to deal with multiple values. In this case, this is implemented via an explicit for loop. TIntegrandInner computes exp(g(x, y)) for multiple values of y, but the fixed value of x that is current in the loop in TIntegrandOuter. This value x(i) play both the role of a parameter into g(x, y) and of an integration limit. Variables x and i are shared between the outer and the nested function.
Almost there! We have the integrand, only the outermost integration needs to be performed:
T = integral(#TIntegrandOuter, 0, inf);
This is a very convoluted implementation, which is not very elegant, and probably not very efficient. Again, whether results of this approach prove to be useful needs to be tested in practice. However, I don't see any other way to implement these numeric integrations in Matlab in a better way in general. For specific choices of f(x, y, v) and p(v), there might be possible improvements.