Mathematica Loop with Dot Product

Click For Summary

Discussion Overview

The discussion revolves around a Mathematica implementation issue involving nested loops and dot products between vectors in two matrices, TT and KK. Participants are exploring the correct syntax and logic for performing these calculations within a loop, while addressing potential errors in the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an attempt to compute dot products between rows of matrix TT and elements of matrix KK, noting that an error occurs when the calculation is performed in a loop.
  • Another participant suggests that a missing comma in the loop syntax may be causing the error, and after correcting it, they report no errors but do not verify the numerical correctness.
  • A later reply indicates that the initial suggestion did not resolve the issue and introduces a new algorithm involving a different matrix D and vectors Mu, seeking clarification on the intended output.
  • Additional participants point out various potential syntax errors in the code, including missing commas, incorrect loop conditions, and issues with variable naming, while providing corrected code snippets for review.
  • There is a discussion about the importance of simplifying code examples for clarity and ensuring that all necessary components are included for others to replicate the issue accurately.

Areas of Agreement / Disagreement

Participants express differing views on the source of the error in the original code, with some suggesting syntax issues while others propose that the logic may also need reevaluation. The discussion remains unresolved regarding the correctness of the numerical results and the overall implementation.

Contextual Notes

Participants highlight limitations in the clarity of the original code due to formatting issues and the complexity of the matrices involved, which may affect understanding and troubleshooting.

Who May Find This Useful

Individuals working with Mathematica who are interested in matrix operations, nested loops, and debugging code related to vector calculations may find this discussion beneficial.

brydustin
Messages
201
Reaction score
0
Basically, I'm trying to get the various entries of TT to be dotted with the various entries of KK. Each row of TT is a vector while in KK the elements are vectors.
So I'm working with different dimensions, but should be able to "call" them still using the right command. My attempt is below, and when I do the calculation in a loop, I get an error, however if I try it outside of a loop, for example:
Dot[TT[[2]], KK[[2, 3]]]
I don't get error, this equals = 0.86



=======
TT = {{-0.8, 0.4, -0.1}, {-0.1, 0.8, 0.4`}, {-0.5, 0.78`, -0.4}, {0.1,
0.18, -0.9}, {0.8, -0.1`, -0.4}, {0.7, -0.3`, 0.55`}} ;

KK = {{{0.`, 0.`, 0.`}, {-7.1`, 0.7`, -0.1`}, {-13.`, 0.8`,
0.8`}, {-18.`, -2.`, 2.2`}, {-21.`, -6.`, 3.`}, {-26.`, -10.`,
2.`}}, {{0.`, 0.`, 0.`}, {0.`, 0.`, 0.`}, {-5.`, 0.`,
0.9`}, {-11.1`, -2.8`, 2.3285`}, {-14.`, -7.`,
3.`}, {-19.`, -11.`, 3.`}}, {{0.`, 0.`, 0.`}, {0.`, 0.`,
0.`}, {0.`, 0.`, 0.`}, {-5.1`, -2.`, 1.`}, {-8.`, -7.`,
2.`}, {-13.`, -11.`, 2.`}}, {{0.`, 0.`, 0.`}, {0.`, 0.`,
0.`}, {0.`, 0.`, 0.`}, {0.`, 0.`, 0.`}, {-3.`, -4.`,
1.`}, {-8.`, -8.`, 0.`}}, {{0.`, 0.`, 0.`}, {0.`, 0.`, 0.`}, {0.`,
0.`, 0.`}, {0.`, 0.`, 0.`}, {0.`, 0.`, 0.`}, {-4.`, -3.`,
0.`}}, {{0.`, 0.`, 0.`}, {0.`, 0.`, 0.`}, {0.`, 0.`, 0.`}, {0.`,
0.`, 0.`}, {0.`, 0.`, 0.`}, {0.`, 0.`, 0.`}}}


JJ = IdentityMatrix[6];
For[i = 1, i < 6, i++,
For[j = i + 1, j < 6, j++
JJ[[i, j]] = (Dot[TT[], KK[[i, j]]] -
Dot[TT[[j]], KK[[i, j]]])/(Norm[KK[[i, j]]])^3
]]
 
Physics news on Phys.org
Is it as simple as you left a comma off the end of this next line:
For[j = i + 1, j < 6, j++

If I append a comma to the end of that and make no other changes I get a result with no error, but I have not checked whether the numerical value is correct or not.

I see another post you made that appears to have that same problem.
 
Bill Simpson said:
Is it as simple as you left a comma off the end of this next line:
For[j = i + 1, j < 6, j++

If I append a comma to the end of that and make no other changes I get a result with no error, but I have not checked whether the numerical value is correct or not.

I see another post you made that appears to have that same problem.
No that didn't fix the problem, actually that was just a mistype in this blog, sorry about that.
The first entry was confusing to "see" because of the dimensions of the matrices so suppose that we have the following algorithm:

For[i = 1, i < m, i++,
For[j = i + 1, j < m,
If[Dot[\[Mu][[j, All]], \[Mu][[i, All]]] < 0, flip = -1, flip = 1]
Z[[i, j]] = (flip*Dot[D[[i, j]], \[Mu][[i, All]]] -
1*(flip)*Dot[D[[i, j]], \[Mu][[j, All]]])/(Norm[D[[i, j]]])^3;
]]

In D (the first matrix) the upper triangle entries are triples (vectors), and the lower triange (below the diagonal) (single value) are(is) value(s) are scalar elements.
The elements of Mu are vectors (row by row, and there are only two here) -- sorry for the poor appearanc, I'm new to LaTeX-like typing.

Basically the idea is that Z should be an upper triangle matrix whose entries are all dot product permutations of each matrix. Does my question make sense? thanks again for your help

[tex]D=\left(\begin{array}{cc} {{0., 0., 0.}} & {{-4.34, 5.84, -1.31}} 1\\ 0 & {{0., 0., 0.}}\end{array}\right)[/tex]

[tex]Mu=\left(\begin{array}{cc} {1 , 2, -3}\\ { 4, 5 , 6}\end{array}\right)[/tex]
 
There appears to be a missing ,j++ at the end of your For[j=i+1 line.
There appears to be an error and j<m should be j<=m.
There appears to be an error if you are using D as the name of a matrix.
There appears to be a missing ; at the end of your If[].

If all my guesswork and changes are correct then this might be what you want.

In[1]:= D={{{0,0,0},{-4.34,5.84,-1.331}},{0,{0,0,0}}};
From In[1]:= Set::wrsym : Symbol D is Protected. More…

In[2]:= d={{{0,0,0},{-4.34,5.84,-1.331}},{0,{0,0,0}}};
mu={{1,2,-3},{4,5,6}};
m=2;
z={{0,0},{0,0}};
For[i=1,i<m,i++,
For[j=i+1,j<=m,j++,
If[Dot[mu[[j,All]],mu[[i,All]]]<0,flip=-1,flip=1];
z[[i,j]]=flip*(Dot[d[[i,j]],mu[[i,All]]]-Dot[d[[i,j]],mu[[j,All]]])/(Norm[d[[i,j]]])^3;
]
]

In[7]:= z
Out[7]= {{0,-0.0184804},{0,0}}

Is that result exactly correct? And is the result exactly correct for several larger examples?

When making a posting here I

Simplify the notebook down to contain everything it takes to document and demonstrate the problem, nothing more and nothing less, and the desired output for a specific example just large enough to really show what is needed, CAREFULLY manually created if necessary.

Manually go through and eliminate all the "desktop publishing bold blinking reverse italic Olde English fonts and non-ascii characters." The fancier I try to "desktop publish" something the more likely it is that someone trying to help me is just going to have to type the whole thing in all over again just to get Mathematica to accept it.

Kill the kernel and restart that.

Evaluate every cell in the notebook in order.

Select all the cells.

Copy from Mathematica and paste into the forum.

Carefully manually undo any of the wordwrapping done by Mathematica or the forum and any non-ascii characters I might have missed in the step above.

If I do all that without making any mistakes then anyone should be able to read and understand the code and even scrape it, paste it into Mathematica, eliminate the In[]:= and Out[]= that let's you distinguish my input from output, run it and get exactly the same result that I have.

If someone knows enough they can violate one or more of those rules and still get a correct answer to their question on the first try, but if they know enough to violate the rules without making any mistakes then they probably don't need to be asking the question in the first place.
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K