0
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 13, 2014 at 13:37
  • $\begingroup$ Sum[#1 ((2 - #2) #3 + (#2 - 1) #1) &[Log[x1[[i, 1, 1]]], x1[[i, 1, 2]], Log[x1[[i, 2, 1]]]], {i, 4}] $\endgroup$ Nov 13, 2014 at 13:50
  • $\begingroup$ @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. $\endgroup$
    – daniel
    Dec 22, 2014 at 5:04

1 Answer 1

1
$\begingroup$

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}
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.