Mathematica: Need help to modify the code

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

The forum discussion focuses on modifying Mathematica code to achieve a specific output arrangement. The original code uses the function term[n_Integer, m_Integer] to generate terms, but the user requires the indices of matg to be in descending order. The solution provided involves changing the arguments in the Table command to {i, n, m + 1, -1}, which successfully produces the desired output format. This adjustment ensures that the terms are generated in reverse order, aligning with the user's requirements.

PREREQUISITES
  • Familiarity with Mathematica programming language
  • Understanding of function definitions in Mathematica
  • Knowledge of the Apply and Sum functions in Mathematica
  • Basic grasp of indexing and ordering in programming
NEXT STEPS
  • Explore advanced Mathematica function manipulation techniques
  • Learn about the Table function and its argument configurations in Mathematica
  • Investigate optimization strategies for Mathematica code performance
  • Study the use of Dot products in Mathematica for matrix operations
USEFUL FOR

Mathematica users, programmers looking to optimize code output, and anyone interested in mathematical programming and function manipulation.

kaizen.moto
Messages
94
Reaction score
0
Hi all,
I need help to edit the code so that the arrangment of the outputs match the one that Iam looking for.

For instance:
term[n_Integer, m_Integer] :=
Apply[Dot, Table[matg[], {i, m + 1, n}]].hl[[m]] /; (n >= m);
Subscript[lay, s_] := Sum[term[s, i], {i, s}];

In[1]: Subscript[lay,3]
Out[1]: matg[[3]].hl[[2]] + matg[[2]].matg[[3]].hl[[1]] + hl[[3]]

In[2]: Subscript[lay,4]
Out[2]:matg[[4]].hl[[3]] + matg[[3]].matg[[4]].hl[[2]] +
matg[[2]].matg[[3]].matg[[4]].hl[[1]] + hl[[4]]

However, I need to get the right code of the above, so that the output that Iam looking will look like this,

In[3]: Subscript[lay,3]
Out[3]: matg[[3]].hl[[2]] + matg[[3]].matg[[2]].hl[[1]] + hl[[3]]

In[4]: Subscript[lay,4]
Out[4]: matg[[4]].hl[[3]] + matg[[4]].matg[[3]].hl[[2]] +
matg[[4]].matg[[3]].matg[[2]].hl[[1]] + hl[[4]];


As you can see the above pattern, I wish the output (the number in the bracket of matg) to be arranged in desecending order.

Please help me to modify the above code and many thanks for any kind helps.

Note: Please ignore the warnings given by the mathematica
 
Physics news on Phys.org
You have to generate factors in reverse order. Just change arguments in Table command:

Code:
term[n_Integer,m_Integer]:=Apply[Dot,Table[matg[[i]],{i,n,m+1,-1}]].hl[[m]]/;(n>=m);
Subscript[lay,s_]:=Sum[term[s,i],{i,s}];
 
It works...Thank you so much.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K