double sum in mathematica - wolfram-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

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.

Summation in 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.

image encryption using henon equation

i want to encrypt pixel value using henon equation :
Xi+2 = 1 - a*(Xi+1)*(Xi+1) + bXi (sorry i can't post image)
where a=1.4, b=0.3, x0=0.01, x1=0.02,
with this code :
k[i+2] =1-a*(Math.pow(k[i+1], 2))+b*k[i]
i can get random value from henon equation
1.00244,
-0.40084033504000005,
1.0757898361270288,
-0.7405053806319072,
0.5550494445953806,
0.3465365454865311,
0.99839222507778,
-0.2915408854881054,
1.1805231444476698,
-1.038551118053691,
-0.15586685140049938,
0.6544223990721852,
. after that i rounded the random value
with this code :
inter[i]= (int) Math.round((k[i]*65536)%256)
i can encrypt the pixel value by XOR with random value (henon).
my question :
there are some negative random value from henon, as we know that there aren't negative pixel value.
so may i skip the negative random value (only save positive random
value) to encrypt original pixel value ?
Thanks
You are using the Hénon sequence as a source for pseudo-random numbers, right?
Then you can of course chose to discard negative numbers (or take the absolute value, or do some other fancy thing) - as long as you do the same in encryption and decryption. If there is a specification, it should better be explicit about this.
Maybe you are using Javascript or some other language where % is not modulus, but remainder. If so, see this answer
Three other things to note:
Double-check that you are claculating the right thing. It seems to me that your calculation should read k[i+1] =1-a*(Math.pow(k[i], 2))+b*k[i], since the Hénon sequence only uses the last value.
`
Do you really need to store past values of k? If not, then just use
k =1-a*(Math.pow(k, 2))+b*k
or even better
k = 1 + k * (b - a *k)
(Spoiler warning: This may be the didactical point of an exercise.) The Hénon sequence is chaotic, and floating point errors will sooner or later influence the random numbers. So your random number generator maybe isn't as deterministic as you think.

How do i get Math.Sqrt to return a Bignum and not a Float?

I'm trying to calculate the square root of a really big number in Ruby. The problem I have is that the Math.sqrt function looks like this
sqrt(numeric) → float
If I feed it a really big number, it will give me FloatDomainError: Infinity.
What is the best way to get sqrt() to return a BigNum? Is there perhaps a gem for this or will I have to write my own function to calculate the square root?
In that case, what is the easiest way to go about doing this? Taylor series? The square roots of the numbers will always be integers.
There is a simple way to calculate the square root of an integer, which results in an integer:
To find the square root of a number, set M and P to that number.
Then calculate (M+P/M)/2, rounding each division down.
If M equals or is less than the result, use M as the square root;
otherwise, set M to the result and repeat this process at step 2.
This approach may be inefficient for big numbers, though, so try it and see.
EDIT:
Here's the Ruby implementation:
def mysqrt(x)
return 0 if x==0
m=x
p=x
loop do
r=(m+p/m)/2
return m if m<=r
m=r
end
end

Resources