Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: got problem with substitution

  1. Jul 28, 2011 #1
    Dear all,

    I have a problem with my code using the substitution. The code works perfectly without substitution however it does not work with substitution. I mean, I got the results of sub[1], sub[2] and sub[3].

    But using the code replacement, I got sub[1] = sub[2] = sub[3], which are not true.

    Please help me fix this problem.

    Thank you.

    Attached Files:

  2. jcsd
  3. Jul 28, 2011 #2
    If I strip your 146 kbytes of nearly identical code down to the few lines that differ I get


    In[1]:= sub[1]=eDzstar.topmat;sub[2]=eDzstar.eDz1.topmat;sub[3]=eDzstar.eDz1.eDz1.topmat;

    In[3]:= {sub[1],sub[2],sub[3]}
    Out[3]= {eDzstar.topmat,eDzstar.eDz1.topmat,eDzstar.eDz1.eDz1.topmat}

    (*Now kill the kernel and begin fresh*)

    "Doesn't Work"

    In[1]:= reps={eDzstar-> eDz1,eEzstar-> eEz1};
    sub[r_Integer?(4>#>1||#>= 14&)]:= eDzstar.(Distribute/@(sub[r-1]/.reps));
    sub[r_Integer?(14>#>= 4&)]:= eEzstar.(Distribute/@(sub[r-1]/.reps));

    In[4]:= {sub[1],sub[2],sub[3]}
    Out[4]= {eDzstar.topmat,eDzstar.eDz1.topmat,eDzstar.eDz1.eDz1.topmat}

    Can you more clearly point out with just these ten lines of code what isn't working?

    The way you write some of your code makes it very difficult for me to understand what you are trying to accomplish and why you are doing things the way you do.

    For example, why are you using Distribute? Is there some essential reason you are using that or did you just think that might have something to do with what you really want?

    Next, if you have one version that works then why are you producing another more complicated and difficult to understand version just to have it not work?

    I'm just guessing here, but is this anything like what you are really actually trying to accomplish?

    sub[r_Integer?(4>#>=1||#>=14&)]:= Dot[eDzstar,Sequence@@Table[dEz1,{r-1}],topmat];
    sub[r_Integer?(14>#>=4&)]:= Dot[eEzstar,Sequence@@Table[eEz1,{r-1}],topmat];

    Does that work?
    Last edited: Jul 28, 2011
  4. Jul 29, 2011 #3
    Thank you for your reply.
    The main different between code that works (Code 1) and doesn't work (Code 2) is clearly that in the Code 1, I have expressed the expressions of sub[1], sub[2] and sub[3] with eDzstar.topmat, eDzstar.eDz1.topmat and eDzstar.eDz1.eDz1.topmat, respectively. This code1 gives the right outputs.
    However, what I wanted is that I prefer not to display the expression of sub[1], sub[2] and sub[3], instead I rather use the codes reps=.....sub[r_Integer?........ as stated in the Code 2. By using this Code 2 with 'Distribute', I am hoping that I could just simply ask Mathematica to work out what is the definition of sub[1], sub[2] and sub[3] using the 'Distribute' function, i.e. I dont have to express what is sub[1], sub[2] and sub[3] as I did in Code 1.

    Your suggested code does not work too but thank you for your kind suggestion. Your code works perfectly for sub[1], sub[2], sub[3] and sub[4] only. It did not work from sub[5] onwards.

    That is the reason I attached the 146kilobytes of codes just to show that Code 2 is not working if I have assigned the material properties and values. The outputs from Code 2 as mentioned showed that sub[1] = sub[2] = sub[3] which are not the case.

    However, without giving the material definitions, I could get the same results as your 10 lines codes using Code 2.

    To summarise my findings, I am trying to use my Code 2 with 'Distribute' function so that I could determine the values of sub[1], sub[2] and sub[3] and so on without defining them inside the code. And the outputs from Code 2 suppose to match with Code 1.

    Bear in mind that, these should be done inside the Do-Loop.(or Sum). Please let me know how to fix my problems. Thank you.
    Hope this helps.
  5. Jul 29, 2011 #4


    Staff: Mentor

    So use code1. In life there is no partial-credit for an aesthetically pleasing chunk of incorrect code.
  6. Aug 1, 2011 #5
    You have been struggling with this and asking people to fix your 150 kbyte Do loop for many months with no success. All replies have been unacceptable or incorrect.

    After long thought I think the best way for you to fix your problems is to pay an expert in Mathematica programming to produce a program that will be acceptable to you.

    Show the experts all the posts and replies you have received here and anywhere else that you have been asking for help. This will help them understand you and your expectations. You do not want to begin paying someone and only after weeks have them discover that they cannot produce something that is acceptable to you. I believe it is essential that they understand that they must not just produce a program that solves your problem, but that they must write the program in exactly the way you wish to have it done and that it solves your problem.

    You do not want to hire someone who is inexpensive or inexperienced. If they are not an expensive expert who understands who they are dealing with I fear they will not be able to produce a satisfactory program.

    This will cost you substantial money. But it will perhaps be cheaper than you having to spend months or years learning how to program in Mathematica to be able to accomplish your goal. Mathematica is in many ways not an easy language to learn and use.

    I hope this works out for you.
    I am not for hire for this project.
    Last edited: Aug 1, 2011
  7. Aug 1, 2011 #6


    Staff: Mentor

    I am :smile: And I fit the expensive and experienced qualifications mentioned above!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook