I'm new to Mathematica but have been trying to write a code that will define new variables based off of variables preset by a user.

I'm allowing them to define a variable pij where $i=3,...,(n-1)$ (where $n$ is also specified by the user) and $j=0,1,2,3$.

i.e, $p23=1/5$, $p42=4$ etc;

I would like to define a new variable,

p20 = Sum[Product[pij, {i, 2, (n - 1), 1}], {j, 1, 3, 1}]

where it would recall the variables set beforehand to perform the sum and product calculations. Any tips?

  • $\begingroup$ You can define functions that remember their values. See, for example reference.wolfram.com/language/tutorial/… $\endgroup$ – user18792 Sep 18 '17 at 9:56
  • $\begingroup$ Welcome! To make the most of Mma.SE start by taking the tour now. It will help us to help you if you write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Sep 18 '17 at 10:23
  • $\begingroup$ There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. Participation is essential for the site, please come back to do your part tomorrow $\endgroup$ – rhermans Sep 18 '17 at 10:26

It seems to me that you are after indexed variables. You may want to see this answer and follow the link for general discussion about "indexed variables".

Basically instead of using a Ownvalues for a collection of different symbols you use Downvalues for a single symbol. In practice that implies that your $p23=1/5$ translates to p[2,3]=1/5 instead of p23=1/5.

For example:

p[2, 3] = 1/5;
p[4, 2] = 4;
p[2, 2] = π;
p[3, 1] = x;

p[2, 0] = Sum[Product[p[i, j], {i, 2, 4, 1}], {j, 1, 3, 1}]
4 π p[3, 2] + x p[2, 1] p[4, 1] + 1/5 p[3, 3] p[4, 3] 

For an advanced understanding you could read this answer (thanks to comment by @jjc385), the documentation for Symbol Handling and for Manipulating Value Lists and try to familiarize yourself with the differences between OwnValues, DownValues and UpValues.

  • 1
    $\begingroup$ Perhaps useful to OP: A fairly readable explanation of OwnValues and DownValues. $\endgroup$ – jjc385 Sep 18 '17 at 11:16
  • $\begingroup$ Thank you! Just made the appropriate additions to my code and it has worked perfectly! I'll do my reading, appreciate it alot! $\endgroup$ – Nyika Sep 18 '17 at 12:13
  • $\begingroup$ @Nyika I'm happy to help, and glad you appreciate it. There is a way to return the favour, not to me, but to the community. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes. Did something cool or pedagogical? Share what you have learned! And next time, as I wrote in the other comment, do wait a day before accepting answers to encourage other people with better ideas to write their own approach and solution to your problem.. $\endgroup$ – rhermans Sep 18 '17 at 12:33

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.