Mathematica: please from the experts

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

The discussion centers on resolving issues with a Mathematica function designed to compute sums based on user-defined expressions for variables u1, u2, v1, and v2. The original code provided by the user was corrected by another participant, who suggested using matrix operations for simplification. The user acknowledged the effectiveness of the revised code but expressed concerns about its practicality due to the complexity of their actual expressions. Ultimately, the user successfully adapted the code to work with their specific requirements, demonstrating the importance of clear variable definitions and avoiding conflicts in Mathematica.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with the concept of modules in Mathematica
  • Knowledge of matrix operations and summation in Mathematica
  • Basic understanding of polynomial expressions and their manipulation
NEXT STEPS
  • Explore advanced Mathematica functions for handling complex expressions
  • Learn about defining and using custom functions in Mathematica
  • Investigate the use of linear algebra techniques in Mathematica for solving equations
  • Study best practices for variable naming and scope management in Mathematica
USEFUL FOR

Mathematica users, mathematicians, and programmers working with complex polynomial expressions and seeking to optimize their code for better performance and clarity.

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
2K
  • · 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