Summation in Mathematica - wolfram-mathematica

I am trying to understand a piece of code I found but I am stuck on this summation. This is part of a larger do loop over n.
UnderoverscriptBox[\(\[Sum]\), \(m = \(-10\)\), \(10\)]\(\(eigenfunctionsort[n, j]\)[\([m + 11]\)] Exp[I*2*\[Pi]*m*x/dp]\)\),{j,1,21}]
What I mainly do not understand is what is happening with the [m+11]. Is that being multiplied with eigen function sort with each step in the sum, or is that simply adding 11 to m at each step in the sum?
Thanks Ben

It looks to me as if your expression contains the following sub-expression
eigenfunctionsort[n,j][[m+11]]
which looks as if:
there is a call to a function eigenfunctionsort with the arguments n, and j; and
the call returns a list of values and the expression [[m+11]] selects the m-plus-11-th element of the list; the doubled square brackets are a short-hand for Mathematica's Part function. Given that m ranges over -10..10 the expression m+11 will range over 1..21.

Related

Maximum sum of sequence

Suppose we have sequence of x numbers and x-1 operators (+ or -), where the order of the numbers and the operators are fixed. For example 5-2-1+3. By different parentheses you get different values. For example (5 - 2)-1+3 = 5, 5-(2-1)+3=7 and so on. I am now interested in the maximum sum and best in linear run-time/memory space.
I think that this problem can be solved with dynamic programming, but I simply don't find a meaningful variant.
What you need here is certainly a dynamic algorithm.
This would work in a recursive way, finding the maximum value that can be gotten for every range.
Algorithm:
You could separate the numbers and the operators into different lists (if the first number is positive add + to the list first).
max_sum(expression, operators):
if len(expression) == 1: return expression
max_value = -float('inf') # minus infinity
length = len(expression)
for i in range(length):
left_exp = max_sum(expression[0:i], operators[0:i])
right_exp = max_sum(expression[i:length], operators[i:length])
value = operator[i].apply(left_exp, right_exp)
if value >= max_value:
max_value = value
return max_value
The main idea of the algorithm is that it checks the maximum sums in every possible range division, goes all the way down recursively and then returns the maximum sum it got.
The pseudo-code doesn't take into account a case where you could get a maximum value by substracting the minimum value of the right expression, but with a few tweaks I think you could fix it pretty fast.
I tried to make the pseudo-code as easy to convert to code as possible out of my head, I hope this helps you.
Let an expression be a sequence of operator-number pairs: it starts with an operator followed by a number, and ends with an operator followed by a number. Your example 5-2-1+3 can be made into an expression by placing a + at the beginning: +5-2-1+3.
Let the head of an expression be its first operator-number pair, and its tail, the rest. The head of +5-2-1+3 is +5 and the tail, -2-1+3.
In this context, let parenthesizing an expression mean placing an opening parenthesis just after the first operator and a closing parenthesis at the end of the expression, like so: +(5-2-1+3). Parenthesizing an expression with a positive head doesn't do anything. Parenthesizing an expression with a negative head is equivalent to changing every sign of its tail: -(5 -2-1+3) = -5 +2+1-3.
If you want to get an extremum by parenthesizing some of its subexpressions, then you can first make some simplifications. It's easy to see that any subexpression of the form +x1+x2+...+xn won't be split: all of its elements will be used together towards the extremum. Similarly, any subexpression of the form -x1-x2-...-xn won't be split, but may be parenthesized (-(x1-x2-...-xn)). Therefore, you can first simplify any subexpression of the first form into +X, where X is the sum of its elements, and any subexpression of the second form into -x1-X, where X is the sum of its tail elements.
The resulting expression cannot have 3 consecutive - operators or 2 consecutive + operators. Now, start from the end, find the first subexpression of the form -a-b, -a+b-c, or -a+b, and compute its potential minimum and its potential maximum:
min(-a-b) = -a-b
max(-a-b) = -(a-b)
min(-a+b-c) = -(a+b)-c
max(-a+b-c) = -a+b-c if b>=c, max(-a+b-c) = -(a+b-c) if b<=c
min(-a+b) = -(a+b)
max(-a+b) = -a+b
Repeat by treating that subexpression as a single operator-number pair in the next one, albeit with two possible values (its two extrema). This way, the extrema of each subsequent subexpression is computed until you get to the main expression, of which you can simply compute the maximum. Note that the main expression may have a positive first pair, which makes it a special case, but that's easy to take into account: just add it to the maximum.

Calculate the sum of the elements of an elementwise product of two matrices in Maxima

What's in the title and I've tried thus far:
sum(lambda([i,j], M[i,j] * Ma[i,j]))
This expression gives me the Wrong number of arguments error. Doesn't sum() operate on lists, or am I missing something like an expression in the sum function call?
Turns out the answer was rather simple:
lsum(i,i, list_matrix_entries(M3 * Ma))
This essentially translates to (\sum_{i\in{set of the elements of M*Ma}} i) in LaTeX.

Checking all elements of array for a logical condition in fortran

I want to check all rows of an array for a logical condition. I used function ALL as described in GNU GCC guide https://gcc.gnu.org/onlinedocs/gfortran/ALL.html
Here is a sample code:
program test3
implicit none
real, allocatable, dimension (:,:) :: mat1
integer :: i,j,k,r
logical :: lg
r=3
allocate(mat1(r,r))
mat1=transpose( reshape( (/-1,-2,-3,-4,-5,-6,-7,-8,-9/), (/3,3/)))
lg=all (abs(mat1)<10,1)
write (*,*) lg
end program
In this program, I want to check whether absolute value of all elements along all rows is less than 10. But I am getting error
lg=all (abs(mat1)<10,1)
Error: Incompatible ranks 0 and 1 in assignment
Any idea about this error or how to do this check?
Error: Incompatible ranks 0 and 1 in assignment
means that you are trying to assign a rank-1 array to a scalar variable.
In this case lg is your scalar left-hand side. As you want to test the condition against each row (as supported by using the [dim=]1 specifier) it makes sense for lg to be an array of rank-1 with as many elements as there are rows in mat1.
That said, because Fortran uses column-major storage using ALL(...,dim=1) here is actually giving you the test result along columns. In general, the result of ALL(L, dim=n) is of shape [d_1, d_2, ..., d_{n-1}, d_{n+1}, ..., d_m] where the shape of L is [d_1, ..., d_m].
[As noted in another answer the result of ALL(L) is a scalar. If this is what you want here, then I may have something to say about potential confusion with the language of the formal description of ALL.]
Use this to get scalar logical:
lg = all(abs(mat1) < 10)

double sum in mathematica

I want to write this simple code for double sum in mathematica, but it had no answer to me
Actually I want to write the square of the following series(n may be infinity) as a double sum.
try this
Sum[a[i]*b[j-i],{j,0,n},{i,0,j}]
For more information on sum and products in mathematica, have a look at the documentation:
Sum and products
hope it helps
for your second question write:
(Sum[(-1)^(k-1)/k^t,{k,1,n}])^2
Not sure what your update is, but if it's about the infinity just type :
(Sum[(-1)^(k-1)/k^t,{k,1,Infinity}])^2
and if you want the numerical value type :
N[%]
and it will print you the numerical value of your sum
If you want to define a function :
f[n_]:= (Sum[(-1)^(k-1)/k^t,{k,1,n}])^2
the square as a double sum is :
Sum[(-1)^(i-1)/i^t*(-1)^(j-1)/j^t,{i,1,n},{i,1,n}]
and it should be equal with any n to the square (Sum[(-1)^(k-1)/k^t,{k,1,n}])^2

Prolog; try to make fibonacci more effective?

This logic programming is really making a lap dance on my imperative programming skills. This is homework, so please just don't drop me the answer. This is what I have:
fibo(N,1) :-
N < 2,
!.
fibo(N,R) :-
N1 is N-1,
N2 is N-2,
fibo(N1,R1),
fibo(N2,R2),
R is R1+R2.
I'm suppose to make another function that looks like this; fib(N,Value,LastValue).
N is the n'th number, and value is the return value. I don't understand how I can rewrite this using accumulation. And since it counts backwards I don't see how it can "know" a last value before it calculates anything. :s Any input is appreciated.
I could post here the solution, but since that this is homework, it would be counter-productive. Instead, here's a lead:
The problem with the version of Fibonacci that you listed is that it is inefficient. Each call to fibo/2 causes another two calls, but some of these calls calculate the values of the same Fibonacci numbers. For example, in pseudo-code:
(a) fibo(4) -> fibo(3), fibo(2)
(b) fibo(3) -> fibo(2), fibo(1)
(c) fibo(2) -> fibo(1), fibo(0) % called from (a)
(d) fibo(2) -> fibo(1), fibo(0) % called from (b), redundant
To overcome this deficiency, you were asked to rephrase Fibonacci in terms of returning not just the last value, but the last two values, so that each call to fib/3 will cause only a single recursive call (hence calculate the Fibonacci series in linear time). You'll need to change the base cases to:
fib(1,1,0).
fib(2,1,1).
I'll leave the recursive case to you.
For the impatient
Here is the recursive case as well:
fib(N, Val, Last) :-
N > 2,
N1 is N - 1,
fib(N1, Last, Last1), % single call with two output arguments,
% instead of two calls with one output argument
Val is Last + Last1.
See the related discussion:
Generalizing Fibonacci sequence with SICStus Prolog
and consider ony's very good solution using finite domain constraints from there.
Perhaps using tail recursion is a good option
edit:
Instead of breaking fib(6) into fib(5)+fib(4) you might try something like fib(6) = fib(6,0,0) the first parameter is the count of steps, when it reaches 0 you stop, the second parameter the last value you calculated, and the third parameter is the value to calculate which is equal to the sum of current second and third parameters (with the exception of the first step, in which 0 + 0 will be 1)
So to calculate you set the second parameter at each call and acumulate in the third so fib(6,0,0) => fib(5,0,1) => fib(4,1,1) => fib(3,1,2) => fib(2,2,3) => fib(1,3,5) => fib(0,5,8) then you return 8
In that method you dont actually have to save in the stack the adress return, avoiding stack overflow
Remember that there is another way to calculate the Fibonacci sequence: starting from the base case and moving up.
Right now, to calculate fib(n), you add fib(n-1) and fib(n-2). Instead, flip that around and calculate fib(0) and fib(1) based on the definition of the Fibonacci sequence, and build up from that.
You almost already have it. Just rewrite:
fibo(N, Value) :-
N1 is N-1, N2 is N-2,
fibo(N1, LastValue),fibo(N2, SecondToLastValue),
Value is LastValue + SecondToLastValue.
in terms of
fibo2(N, Value, LastValue):- ...
I don't understand how I can rewrite
this using accumulation
Just don't, this is not needed (although it's possible to do so).

Resources