I have a small query and a related problem regarding the working methodology of Mathematica under set delayed/Do functions. Here is a simplified version of my problem.

a[0] = IdentityMatrix[9]
a[m_] := f[m]*a[0]
b[m_] := Inverse[a[m]]
Do[If[Tr[b[m - 1]] < 1, f[m] = x*x /. x -> m, 
  f[m] = x*x*x /. x -> m]; Print[a[m]], {m, 1, 3}]

a[m] is a 9 X 9 matrix having elements being functions of f[m], whereas for m = 0, a[0] is known in numerical form. a[m] passes through multiple complex steps involving inverse etc. and finally lands in a "Do" loop. Under "Do' loop, the value of f[m] is determined for successive values of m by using the matrix a[m-1] obtained in last step.

Loop works fine for m=1 cycle as a[0] is known in numerical form. The problem arises for higher m values when a[m] gets more and more complex, as under set delayed condition a[m] is carried to the "Do" loop in algebraic form (please correct me if I am wrong !) and there it is converted to the numerical form by substituting f[m]. Since in between lie the terms involving inverse etc. (of a matrix with algebraic terms), the code runs forever with no output.

Could there be a way to carry the value of R[m] to the equation of a[m] and get it converted to numerical form before carried through multiple steps to reach for next m cycle under Do loop, instead of current scenario ? Could there be another solution to this problem !

Will be thankful for any suggestion !

  • $\begingroup$ I'm not sure I understand your problem. Can you describe it in words/math formulas? Thanks! $\endgroup$ – AccidentalFourierTransform Jul 22 '19 at 13:53
  • $\begingroup$ thanks for your interest ! Problem is very simple. a[m] is a 9X9 matrix with algebraic elements. Later terms require its inverse, which is very difficult to calculate if its elements are not numbers. Substituting R[m] can convert its elements to numerical values but it happens only in the last step under Do loop. I am looking for a method by which I can take the value of R[m] from Do loop and substitute it for a[m] before it goes through the inverse operation. $\endgroup$ – user49535 Jul 22 '19 at 15:26
  • $\begingroup$ looks like f[m] is a scalar, yes? $\endgroup$ – kglr Jul 22 '19 at 20:15
  • $\begingroup$ I'm a bit lost here, but this seems to be an iterated function system, where the result depends on the previous result. Nest and related functions handle this pattern better than Do. $\endgroup$ – John Doty Jul 22 '19 at 20:50

Looks like f[m] is a scalar.

If that's the case, Tr[f[m] mat] is f[m] Tr[mat] and Tr[Inverse[f[m] mat] is (1/f[m])Tr[Inverse[mat]]. So you need to do inversion only once to get Tr[Inverse[a[0]]]:

trai0 = Tr[Inverse @ a[0]];

Table[If[trai0 < f[m - 1], f[m] = m^2, f[m] = m^3]; a[m], {m, 1, 3}]


Do[If[trai0 < f[m - 1] , f[m] = m^2, f[m] = m^3]; Print[a[m]], {m, 1, 3}]

Note: For the example in OP, (a[0] = IdentityMatrix[9]) Tr[Inverse @ a[0]] is just 9.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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