Mathematica: got problem with substitution

In summary, the problem you are having is that you are trying to use substitution in your code, but it does not work. The code that works without substitution produces the correct outputs, but the code that does not work produces incorrect outputs.
  • #1
kaizen.moto
98
0
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.
 

Attachments

  • Replacement.nb
    146 KB · Views: 379
Physics news on Phys.org
  • #2
If I strip your 146 kbytes of nearly identical code down to the few lines that differ I get

"Works"

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));
sub[1]=eDzstar.topmat;sub[2];sub[3];

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:
  • #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 don't 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.
 
  • #4
kaizen.moto said:
This code1 gives the right outputs.
So use code1. In life there is no partial-credit for an aesthetically pleasing chunk of incorrect code.
 
  • #5
kaizen.moto said:
Bear in mind that, these should be done inside the Do-Loop.(or Sum). Please let me know how to fix my problems.

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:
  • #6
I am :smile: And I fit the expensive and experienced qualifications mentioned above!
 

1. What is Mathematica and how does it work?

Mathematica is a computer program used for mathematical and scientific calculations, data analysis, and visualization. It uses a symbolic language to perform calculations and allows for interactive exploration of data and functions.

2. What is substitution in Mathematica?

Substitution in Mathematica refers to the process of replacing a symbol or expression with another symbol or expression in a mathematical equation or function. It is commonly used to simplify complex expressions or to solve equations.

3. Why am I having trouble with substitution in Mathematica?

There could be several reasons for having trouble with substitution in Mathematica. Some common issues include using incorrect syntax, not defining variables properly, or attempting to substitute into an unsolvable equation.

4. How can I troubleshoot my substitution problem in Mathematica?

To troubleshoot a substitution problem in Mathematica, first check your syntax and make sure all variables are properly defined. You can also use the "Trace" function to see the steps Mathematica is taking in your calculation and identify any errors. Additionally, seeking help from online resources or consulting the Mathematica documentation can also be useful.

5. Can I use substitution to solve any type of equation in Mathematica?

No, substitution may not work for every type of equation in Mathematica. It is most commonly used for linear equations, but may also work for some polynomial, rational, or transcendental equations. However, there are other methods and functions in Mathematica that can be used to solve different types of equations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
246
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
399
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top