Mathematica Loop with Dot Product

In summary, the conversation revolved around an algorithm for calculating a matrix Z, whose entries are dot product permutations of two matrices D and Mu. The code provided in the conversation included a For loop and an If statement, with some minor issues that were pointed out and corrected. The result of the algorithm was also shown for a specific example. The conversation also included some tips for creating a clear and easily understandable post on the forum.
  • #1
brydustin
205
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
  • #2
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.
 
  • #3
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]
 
  • #4
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:
  • #5


I would first commend the attempt to use a loop and the Dot product in Mathematica to perform the desired calculation. However, it seems that there may be some errors in the code that are causing the loop to not run properly. It would be helpful to see the specific error message that is being generated in order to troubleshoot the issue.

One potential issue that stands out is the use of the IdentityMatrix[6] for the variable JJ. This creates a 6x6 matrix of all zeros, which may not be the intended result. It may be helpful to initialize JJ as an empty matrix and then populate it with the calculated values using the loop.

Additionally, it may be helpful to check the dimensions of the vectors being used in the Dot product to ensure that they are compatible and the calculation can be performed.

Overall, it is important to carefully check the code and make sure all syntax and variable assignments are correct in order to successfully run the loop and obtain the desired results.
 

1. What is a Mathematica Loop with Dot Product?

A Mathematica Loop with Dot Product is a programming construct in Mathematica that allows for repeated execution of a set of instructions using a dot product operation. The dot product is a mathematical operation that multiplies two vectors and returns a scalar value.

2. How does a Mathematica Loop with Dot Product differ from a traditional loop?

A traditional loop in programming involves iterating through a set of values or elements and performing a specific action on each one. In contrast, a Mathematica Loop with Dot Product uses the dot product operation to perform a single calculation on a set of vectors, eliminating the need for individual iterations.

3. What are the benefits of using a Mathematica Loop with Dot Product?

Using a Mathematica Loop with Dot Product can greatly simplify and condense code, making it more efficient and easier to read. It also allows for faster execution compared to traditional loops, as the dot product operation is optimized for vector calculations.

4. Can a Mathematica Loop with Dot Product be used for complex calculations?

Yes, a Mathematica Loop with Dot Product can handle complex calculations involving multiple vectors and matrices. It is particularly useful for tasks such as matrix multiplication and linear algebra operations.

5. Are there any limitations to using a Mathematica Loop with Dot Product?

One limitation of using a Mathematica Loop with Dot Product is that it is not suitable for all types of loops. It is best suited for situations where the same calculation needs to be repeated on a set of vectors, rather than performing different actions on each element. Additionally, it may not be the most efficient approach for certain types of calculations, so it is important to consider the specific task at hand before using this method.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
4K
  • Nuclear Engineering
Replies
7
Views
2K
  • Nuclear Engineering
Replies
3
Views
1K
Replies
6
Views
1K
  • Precalculus Mathematics Homework Help
Replies
3
Views
798
Back
Top