1
$\begingroup$

Is there any software where this kind of symbolic calculation is possible?

s = 0
for i = 1 to m
   j = 2 i + a - t
   if (j > 0) s = s + a

I want s as a function of m,a,t, where m,a,t are integers. This is a toy example. In my calculation a lot of if/else, minimum of two values, etc., are involved.

$\endgroup$
4
  • $\begingroup$ Mathematica can certainly handle this, but I don't see this a symbolic calculation if all the actual arguments are going to be integer values. I suggest you elaborate on how you intend to use unbound symbols in your work. $\endgroup$
    – m_goldberg
    Commented Dec 15, 2012 at 1:40
  • $\begingroup$ @m_goldberg perhaps he means to evaluate his statement symbolically into closed form. OP, could you please clarify? $\endgroup$
    – VF1
    Commented Dec 15, 2012 at 1:54
  • $\begingroup$ Otherwise, OP, I recommend taking a look at this. What you wrote in psuedocode can easily be transcribed into MMA. $\endgroup$
    – VF1
    Commented Dec 15, 2012 at 1:59
  • $\begingroup$ Yes, I want closed form of s. Will you kindly explain how to write this code? $\endgroup$
    – user46185
    Commented Dec 15, 2012 at 2:23

1 Answer 1

3
$\begingroup$

If I understand you, then:

Sum[Boole[2 i + a - t > 0] a, {i, 1, m}]

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ I gotta learn how to use Boole better! $\endgroup$ Commented Dec 15, 2012 at 3:28
  • $\begingroup$ @user46185 I see that you Accepted this answer. Were you able to adapt this to your larger problem? You mentioned "a lot of if/else" -- generally you want to use Boole and Piecewise which are mathematical constructs rather than If/Which etc. flow control functions, when using Sum, Product etc. to compute a symbolic equivalent. $\endgroup$
    – Mr.Wizard
    Commented Dec 15, 2012 at 5:33

Your Answer

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

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