hyperpolic function integration in maple - 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/

Related

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.

Solve equation with exponential term

I have the equation 1 = ((π r2)n) / n! ∙ e(-π r2)
I want to solve it using MATLAB. Is the following the correct code for doing this? The answer isn't clear to me.
n= 500;
A= 1000000;
d= n / A;
f= factorial( n );
solve (' 1 = ( d * pi * r^2 )^n / f . exp(- d * pi * r^2) ' , 'r')
The answer I get is:
Warning: The solutions are parametrized by the symbols:
k = Z_ intersect Dom::Interval([-(PI/2 -
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n))], (PI/2 +
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n)))
> In solve at 190
ans =
(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
-(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
You have several issues with your code.
1. First, you're evaluating some parts in floating-point. This isn't always bad as long as you know the solution will be exact. However, factorial(500) overflows to Inf. In fact, for factorial, anything bigger than 170 will overflow and any input bigger than 21 is potentially inexact because the result will be larger than flintmax. This calculation should be preformed symbolically via sym/factorial:
n = sym(500);
f = factorial(n);
which returns an integer approximately equal to 1.22e1134 for f.
2. You're using a period ('.') to specify multiplication. In MuPAD, upon which most of the symbolic math functions are based, a period is shorthand for concatenation.
Additionally, as is stated in the R2015a documentation (and possibly earlier):
String inputs will be removed in a future release. Use syms to declare the variables instead, and pass them as a comma-separated list or vector.
If you had not used a string, I don't think that it would have been possible for your command to get misinterpreted and return such a confusing result. Here is how you could use solve with symbolic variables:
syms r;
n = sym(500);
A = sym(1000000);
d = n/A;
s = solve(1==(d*sym(pi)*r^2)^n/factorial(n)*exp(-d*sym(pi)*r^2),r)
which, after several minutes, returns a 1,000-by-1 vector of solutions, all of which are complex. As #BenVoigt suggests, you can try the 'Real' option for solve. However, in R2015a at least, the four solutions returned in terms of lambertw don't appear to actually be real.
A couple things to note:
MATLAB is not using the values of A, d, and f from your workspace.
f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
Passing additional options of 'Real', true to solve gets rid of most of these extraneous conditions.
You probably should avoid calling the version of solve which accepts a string, and use the Symbolic Toolbox instead (syms 'r')

MuPAD evaluation of local variables, Sum of row of array

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(%)

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

Matlab: linear congruence solver that supports a non-prime modulus?

I'm working on some Matlab code to perform something called the Index Calculus attack on a given cryptosystem (this involves calculating discrete log values), and I've gotten it all done except for one small thing. I cant figure out (in Matlab) how to solve a linear system of congruences mod p, where p is not prime. Also, this system has more than one variable, so, unless I'm missing something, the Chinese remainder theorem wont work.
I asked a question on the mathematics stackexchange with more detail/formatted mathjax here. I solved the issue in my question at that link, and now I'm attempting to find a utility that will allow me to solve the system of congruences modulo a non-prime. I did find a suite that includes a solver supporting modular arithmetic, but the modulus must be prime (here). I also tried stepping through to modify it to work with non-primes, but whatever method is used doesn't work, because it requires all elements of the system have inverses modulo p.
I've looked into using the ability in Matlab to call MuPAD functions, but from my testing, the MuPAD function linsolve (which seemed to be the best candidate) doesn't support non-prime modulus values either. Additionally, I've verified with Maple that this system is solvable modulo my integer of interest (8), so it can be done.
To be more specific, this is the exact command I'm trying to run in MuPAD:
linsolve([0*x + 5*y + 4*z + q = 2946321, x + 7*y + 2*q = 5851213, 8*x + y + 2*q = 2563617, 10*x + 5*y + z = 10670279],[x,y,z,q], Domain = Dom::IntegerMod(8))
Error: expecting 'Domain=R', where R is a domain of category 'Cat::Field' [linsolve]
The same command returns correct values if I change the domain to IntegerMod(23) and IntegerMod(59407), so I believe 8 is unsuitable because it's not prime. Here is the output when I try the above command with each 23 and 59407 as my domain:
[x = 1 mod 23, y = 1 mod 23, z = 12 mod 23, q = 14 mod 23]
[x = 14087 mod 59407, y = 1 mod 59407, z = 14365 mod 59407, q = 37320 mod 59407]
These answers are correct- x, y, z, and q correspond to L1, L2, L3, and L4 in the system of congruences located at my Math.StackExchange link above.
I'm wondering if you tried to use sym/linsolve and sym/solve previously, but may have passed in numeric rather than symbolic values. For example, this returns nonsense in terms of what you're looking for:
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
s = mod(linsolve(A,b),8)
But if you convert the numeric values to symbolic integers, sym/linsolve will keep everything in terms of rational fractions. Then
s = mod(linsolve(sym(A),sym(b)),8)
returns the expected answer
s =
6
1
6
4
This just solves the system linear system using symbolic math as if it were a normal matrix. For large systems this can be expensive, but I'd imagine no more than using MuPAD's numeric::linsolve or linalg::matlinsolve. sym/mod should return the modulus of the numerator of each solution component. I believe that you will get an error if the modulus and the denominator are not at least coprime.
sym/solve can also be used to solve this in a similar manner:
L = sym('L',[4,1]);
[L1,L2,L3,L4] = solve(A*L==b);
s = mod([L1;L2;L3;L4],8)
A possible issue with using either sym/solve or sym/linsolve is that if there are multiple solutions to the linear congruence problem (as opposed to the linear system), this approach may not return all of them.
Finally, using the MuPAD function numlib::ichrem (chinese remainder theorem for integers), here's some code that attempts to obtain the complete solution:
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
m = 10930888;
mf = str2num(strrep(char(factor(sym(m))),'*',' '));
A = sym(A);
b = sym(b);
s = sym(zeros(length(b),length(mf)));
for i = 1:length(mf)
s(:,i) = mod(linsolve(A,b),mf(i));
end
mstr = ['[' sprintf('%d,',mf)];
mstr(end) = ']';
r = sym(zeros(length(b),1));
for i = 1:length(b)
sstr = char(s(i,:));
r(i) = feval(symengine,'numlib::ichrem',sstr(9:end-2),mstr);
end
check = isequal(mod(A*r,m),b)
I'm not sure if any of this is what you're looking for, but hopefully it might be helpful. I think that it might be a good idea to put in a enhancement/service request with the MathWorks so that MuPAD and the other solvers can handle systems better in the future.