Mathematica: please from the experts

  • Context: Mathematica 
  • Thread starter Thread starter kaizen.moto
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around a Mathematica coding problem related to the computation of sums involving variables defined in terms of parameters m and n. Participants explore different approaches to implement the function while addressing issues of practicality and complexity in their expressions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a function f[m, n] that aims to compute sums of expressions u1, u2, v1, and v2 defined in terms of m and n.
  • Another participant questions the correctness of the expected output values provided by the original poster, suggesting an alternative approach using matrix notation.
  • A participant acknowledges the correctness of the alternative code but expresses difficulty in adapting it to their more complex, real-world expressions.
  • Some participants discuss the nature of the expressions, with one noting that they consist of non-linear equations and high-degree polynomials, while another asserts that polynomials of any order can be treated as linear in the context of certain mathematical techniques.
  • There are suggestions to define u1 and v1 as functions within the Module to manage complexity, with a caution to avoid variable name conflicts.
  • One participant reports success in getting their code to work after adjustments, expressing gratitude for the support received.
  • Another participant offers programming assistance, indicating a willingness to help further.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the expressions involved, particularly regarding linearity and the complexity of the original problem. While some solutions are proposed, there is no consensus on the best approach for the original poster's specific case.

Contextual Notes

Participants mention that the original expressions for u1, u2, v1, and v2 are lengthy and complex, which may affect the implementation of the proposed solutions. The discussion includes unresolved issues regarding the adaptation of simpler solutions to more complicated scenarios.

Who May Find This Useful

Individuals working with Mathematica, particularly those dealing with complex mathematical expressions and seeking assistance with coding challenges in symbolic computation.

kaizen.moto
Messages
94
Reaction score
0
Dear all,

Please help me to fix this problem:

f[m_, n_] :=
Module[{u1, u2, v1, v2, sumu1, sumu2, sumv1, sumv2}, u1 = 3 m + 2 n;
u2 = 2 m - 5 n; v1 = 4 m - 3 n; v2 = 6 m + 3 n;
sumu1 = Sum[u1[i, n], {i, 1, m, 2}];
sumu2 = Sum[u2[i, n], {i, 1, m, 2}];
sumv1 = Sum[v1[m, j], {j, 1, n, 2}];
sumv2 = Sum[v2[m, j], {j, 1, n, 2}] {sumu1, sumu2, sumv1, sumv2}];

Do[f[m, n]; Print[f[m, n]], {m, 1, 3, 2}, {n, 1, 3, 2}]

Definitions: the idea is that sumu1(1,1) = u1(1,1); sumu1(1,3) = u1(1,3); sumu1(3,1) = u1(1,1) + u1(3,1); sumu1(3,3) = u1(1,3) + u1(3,3)

and for sumu2(1,1) = u2(1,1); sumu2(1,3) = u2(1,3); sumu2(3,1) = u2(1,1) + u2(3,1); sumu2(3,3) = u2(1,3) + u2(3,3)

However for sumv1(1,1) = v1(1,1); sumv1(1,3) = v1(1,1) + v1(1,3); sumv1(3,1) = v1(3,1) ; sumv1(3,3) = v1(3,1) + v1(3,3)

and similarly for sumv2(1,1) = v2(1,1); sumv2(1,3) = v2(1,1) + v2(1,3); sumv2(3,1) = v2(3,1) ; sumv2(3,3) = v2(3,1) + v2(3,3)


The correct answers I should be getting from the Do-loop are:

f(1,1) = { 5, -3 , 1 , 9}
f(1,3) = { 9 , -13 , -4 , 24 }
f(3,1) = {15 , -2 , 11 , 19 }
f(3,3) = { 24 , -22 , -2 , 42 }

Thank you for any kind help.
 
Physics news on Phys.org
kaizen.moto said:
The correct answers I should be getting from the Do-loop are:

f(1,1) = { 5, -3 , 1 , 9}
f(1,3) = { 9 , -13 , -4 , 24 }
f(3,1) = {15 , -2 , 11 , 19 }
f(3,3) = { 24 , -22 , -2 , 42 }
Are you sure these are correct?

Try this:
Code:
u = {{3, 2}, {2, -5}};
v = {{4, -3}, {6, 3}};
f[m_, n_] :=
 Module[{sumu, sumv},
  sumu = Sum[u.{i, n}, {i, 1, m, 2}];
  sumv = Sum[v.{m, j}, {j, 1, n, 2}];
  Flatten[{sumu, sumv}]
];
Do[Print[f[m, n]], {m, 1, 3, 2}, {n, 1, 3, 2}]
 
Apologise for my mistakes. You are absolutely right. Your revised code is working perfectly for the above simplified exercise.

However, due to the real application, working your code with my case, its not practical. One of the reasons, my actual expression for u1, u1, v1 and v2 consisting of variables m and n (and also some other constants) are quite lengthy. Its going to be tedious to rerrange them collectively in a single expression (such as u={{3,2},{2,-5}).

I still prefer to express u and v in a separate terms, i.e. u1, u2, v1 and v2 as per my original version.

I have tried to use your suggested code but it gives funny looking outputs.

f[m_, n_] :=
Module[{u1, u2, v1, v2, sumu1, sumv1, sumu2, sumv2}, u1 = 3 m + 2 n;
u2 = 2 m - 5 n; v1 = 4 m - 3 n; v2 = 6 m + 3 n;
sumu1 = Sum[u1.{i, n}, {i, 1, m, 2}];
sumv1 = Sum[v1.{m, j}, {j, 1, n, 2}];
sumu2 = Sum[u2.{i, n}, {i, 1, m, 2}];
sumv2 = Sum[v2.{m, j}, {j, 1, n, 2}];
Flatten[{sumu1, sumu2, sumv1, sumv2}]];
Do[Print[f[m, n]], {m, 1, 3, 2}, {n, 1, 3, 2}].

Is there any other way, to get the right code?
 
kaizen.moto said:
However, due to the real application, working your code with my case, its not practical. One of the reasons, my actual expression for u1, u1, v1 and v2 consisting of variables m and n (and also some other constants) are quite lengthy.
Are the expressions always linear, as in this example?

kaizen.moto said:
Is there any other way, to get the right code?
Of course. There are always an infinite number of ways.
 
all the expressions of u1, u2..., v1, v2,... consists of non linear equation. In fact, some of them have a high very degree of polynomial expressions. Actually, I was trying to get a single expression of u or v in terms of other variables including m, n and other constants from Mathematica, but it took me ages to get this expression. The program is still running for many hours to get u's and v's. From this fact, that u's and v's are pretty much high degree polynomial of m and n variables.

In the meantime, Iam tyring to express u's and v's interms of a function such as u1[m_,n_]:=...v1[m_,n_]:=...inside the Module.

I hope it could be done.
 
kaizen.moto said:
all the expressions of u1, u2..., v1, v2,... consists of non linear equation. In fact, some of them have a high very degree of polynomial expressions.
I am a little confusesd by this response. Polynomials of any order are linear. The sum of two polynomials of order k is a polynomial of order k, and the product of a scalar and a polynomial of order k is a polynomial of order k.

The reason that I ask is that if you can cast your problem in terms of a system of linear equations then you open the door to the use of some very powerful pre-packaged computer techniques:

http://en.wikipedia.org/wiki/System_of_linear_equations

kaizen.moto said:
In the meantime, Iam tyring to express u's and v's interms of a function such as u1[m_,n_]:=...v1[m_,n_]:=...inside the Module.

I hope it could be done.
Certainly, it can be done. You just cannot use m and n as your variables since those are already being used by f. Use, e.g. u1[a_,b_]:=... instead. Alternative you can change f so that it doesn't use m and n, the point is that you need to avoid the conflict between the two.
 
Last edited:
I've got it finally, the code is working now with Module.

Again, thank you so much for your relentless support and guidance. You are a very nice person...you deserve more medals...
 
Thanks, and I am still available for hire to program it. :smile:
 
can I have your e-mail address please, if you don't mind...
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 20 ·
Replies
20
Views
5K