Looking for a generic code using Mathematica

In summary, Dale attempted to help a novice user of mathematics by showing them how to code a dot product, but they changed the name from h1 (HOne) to h1 (HL). Although the concept is correct, there are warnings about h1 (HOne) being longer than the depth of the object. If you look at your original post and Dale's attempt to help you, you see that there are multiple uses of hl (HL if I were to capitalize that to make it obvious), but no uses of h1 (H followed by the digit one to make it obvious). So I would start looking there.
  • #1
kaizen.moto
98
0
Dear all,

Iam looking for a generic code that could express the following functions:

Subscript[mata, 1] = hl[[1]];
Subscript[mata, 2] = matg[[2]].hl[[1]] + hl[[2]];
Subscript[mata, 3] = matg[[3]].matg[[2]].hl[[1]]
+ matg[[3]].hl[[2]] + hl[[3]];
Subscript[mata, 4] =
matg[[4]].matg[[3]].matg[[2]].hl[[1]]
+ matg[[4]].matg[[3]].hl[[2]]
+ matg[[4]].hl[[3]]
+ hl[[4]];
Subscript[mata, 5] = matg[[5]].matg[[4]].matg[[3]].matg[[2]].hl[[1]]
+ matg[[5]].matg[[4]].matg[[3]].hl[[2]]
+ matg[[5]].matg[[4]].hl[[3]]
+ matg[[5]].hl[[4]]
+ hl[[5]];

Please help me to get the right code for the above expressions. It may looks like this:

Subsript[mata,i] = ... where i = 1,2,3,...17.

Thank you for any kind help.
 
Physics news on Phys.org
  • #2
term[n_Integer,m_Integer]:=Product[matg[],{i,m+1,n}] hl[[m]] /;(n>=m)
Subscript[mata,n_]:=Sum[term[n,i],{i,n}]
 
  • #3
Hm, the concept is correct and works, however, there is minor warnings that I need to fix, warnings says:

' Part::partd:Part specification h1[[1]] is longer than depth of object.>>'

Thank you for your kind help
 
  • #4
If you look at your original post and Dale's attempt to help you you see that there are multiple uses of hl (HL if I were to capitalize that to make it obvious), but no uses of h1 (H followed by the digit one to make it obvious) until you tried typing back in what he attempted to show you how to do. So I would start looking there.
 
  • #5
Thank you for the comment, I found that the result of the given code shows the product rather than dot product. I mean after running the code, it gives me for instance
In[1]:= Subscript[mata, 4]
Out[1]:= matg[[4]] matg[[3]] matg[[2]] hl[[1]] + matg[[4]] matg[[3]] hl[[2]] + matg[[4]] hl[[3]] + hl[[4]];

Iam trying to figure out how to make it dot product as per original code shown before.
 
  • #6
term[n_Integer,m_Integer]:=Apply[Dot, Table[matg[],{i,m+1,n}]].hl[[m]] /;(n>=m)
 
  • #7
Thank you again for your help. It works now but I still can't figure out why I still got the warnings. I have tried many many ways to overcome the warnings but still failed including putting back the hl( hL) to h1 (h one).

Iam still wondering though why did you change from h1(h one) to h1 (hL).
 
  • #8
One thing first semester students are taught in introductory programming classes, are not to name things like hl or h1 or matg or mata, but to instead use meaningful names that are not easily confused.

Carefully reading all your posts in this particular thread seems to show that no one but you has written h1 (HOne) in any message except the ones where I was pointing out what I thought the source of your error was.

As I've recommended to you at least once before, in the last year of your asking people to fix your problems you could easily have taken programming language classes and learned to avoid at least some of the problems that you struggle with month after month.

If you knew no mathematics you would not stumble along asking people to do your math for you, but you seem to consider learning programming skills to be exempt from this advice.
 
  • #9
Have you tested the code on a known example to determine if it is giving you the correct result? If so then don't worry about the warning, if not, then you need to figure out a test case. I cannot overemphasize the importance of thouroughly testing each module. That is, in fact, one of the big reasons for splitting your code up into smaller modules, so that you can test each piece separately and isolate problems.
 
  • #10
Dale, first of all, I wanted to say, you are a very nice person and very helpful. Iam very pleased with all your comments and advice from the beginning you helped me until now. You have given me the courage to learn to use mathematica in a such a way that makes me feel enthuastic towards learning mathematica. The way you gave me all the comments are very friendly and always make me happy to learn more and more each day. I just wish that you are awarded more medals than the two.
Secondly, I have no idea that this forum is only suitable for advanced level knowledge of mathematica and no room for posting from novice user of mathematica.

Thank you again for all your wonderful, fantastic guidance and commets.

I wish we could meet one day for a cuppa of coffee!
 
  • #11
Thank you, but even if he irritates you I would seriously consider Bill Simpson's advice. He is a good programmer and offers consistently good suggestions. In particular, it would be very valuable for you to take a programming class, a numerical methods class, or both. Even if it isn't specifically in Mathematica there would be a lot of language-independent programming concepts that would translate.
 

FAQ: Looking for a generic code using Mathematica

What is Mathematica and how does it relate to coding?

Mathematica is a software program used for scientific and mathematical calculations, data analysis, and visualizations. It has a built-in programming language called Wolfram Language, which allows users to write code to perform various tasks.

Can Mathematica be used for generic coding?

Yes, Mathematica can be used for generic coding by utilizing its Wolfram Language. This language is designed to be readable and easy to use, making it suitable for coding tasks in various fields such as physics, engineering, and finance.

How can I find a generic code using Mathematica?

There are several ways to find generic code using Mathematica. You can search for existing code libraries on the Wolfram Community website or browse through the documentation and tutorials provided by Wolfram. You can also write your own code using the built-in Wolfram Language.

What are the advantages of using Mathematica for generic coding?

There are several advantages of using Mathematica for generic coding. Firstly, it has a vast library of built-in functions and algorithms, making it efficient for performing complex calculations. It also has powerful visualization tools, making it easy to present data and results. Additionally, it has a user-friendly interface and a large community for support and collaboration.

Are there any limitations to using Mathematica for generic coding?

While Mathematica is a powerful tool for generic coding, it does have some limitations. For example, it may not be the best choice for large-scale projects or projects that require high-speed computations. Additionally, it may not have the same level of flexibility and customization as other programming languages. However, for most scientific and mathematical coding tasks, Mathematica is a highly capable and efficient option.

Similar threads

Replies
2
Views
2K
Replies
2
Views
693
Replies
5
Views
1K
Replies
1
Views
732
Replies
1
Views
2K
Replies
13
Views
2K
Replies
1
Views
811
Replies
2
Views
3K
Back
Top