0

This is likely my error but I cannot see it. I define a vector:

x1 = {{{2, 2}}, {{2, 1}, {3, 1}}, {{3, 2}}, {{2, 1}, {5, 1}}}

If x1[[ i, 1,2]] is 2, I would like to find $(\log$ x1[[ i,1,1]]$^2$).

If x1[[ i, 1, 2]] is 1, I would like to find $(\log$ x1[[ i,1,1]]$\cdot \log $x1[[ i, 2,1]] ).

There are 4 elements of x1 and I would like to add the total of the log expressions for i = 1 to i = 4.

My code more or less was:

(i = 0; Label[A]; i++;

If [ x1[[ i, 1, 2]] = 2, b2 = Log[ x1[[ i, 1, 1]]]^2 + b1,

b2 = Log[ x1[[ i, 1, 1]]]*Log[ x1[[ i, 2, 1]]] + b1]; b1 = b1 + b2; 
 If [ i < 6, Goto[A], Goto[B]]; Label[B]; ) 

Any suggestions for getting this to work? I will be out for a bit but thanks for any help.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
daniel
  • 519
  • 4
  • 12
  • 1
    This is probably the only other mention of Goto[] outside compiled functions in this site. Perhaps you should revise the programming paradigm you're trying to use. I suggest you may start by reading the answers here and then do a recreational trip through the highest voted questions and answers to get a grip on better Mathematica programming practices. – Dr. belisarius Nov 13 '14 at 13:37
  • Sum[#1 ((2 - #2) #3 + (#2 - 1) #1) &[Log[x1[[i, 1, 1]]], x1[[i, 1, 2]], Log[x1[[i, 2, 1]]]], {i, 4}] – Dr. belisarius Nov 13 '14 at 13:50
  • @belisarius: 'Revision of programming paradigm' is a good way of putting it. It's a priority but a slow process given my schedule. Thanks for reminding me. – daniel Dec 22 '14 at 05:04

1 Answers1

1

You can use Sum and a Which statement to test which Log expression to calculate. Also, remember that equality is checked with ==, not =.

Sum[ Which[
x1[[i,1,2]] == 2, Log[ x1[[i,1,1]] ]^2,
x1[[i,1,2]] == 1, Log[ x1[[i,1,1]] ] * Log[ x1[[i,2,1]] ]
],{i,4}
]
Marius Ladegård Meyer
  • 6,805
  • 1
  • 17
  • 26