Mathematica Help for Negative Numbers and Functional Programming

  • Context: Mathematica 
  • Thread starter Thread starter mzu
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around using Mathematica for mathematical computations, specifically focusing on squaring negative numbers, functional programming, and polynomial expansions with modular arithmetic. Participants seek assistance with syntax and programming techniques in Mathematica.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Michael initially questions why squaring -2 results in -4 in Mathematica, leading to a clarification about the need for parentheses to correctly square negative numbers.
  • Participants share resources for learning functional programming in Mathematica, including online tutorials and books.
  • Michael expresses interest in programming Lego Mindstorms with Mathematica and seeks confirmation about its feasibility.
  • A participant suggests using a loop to define prime numbers between 2 and m², where m is a set ranging from 1 to 200.
  • Another participant proposes a nested loop structure to expand the polynomial (1+x)ⁿ for various values of m and apply modular arithmetic with prime numbers.
  • There is uncertainty regarding the correct syntax for defining prime numbers and applying modular operations in Mathematica.
  • Participants discuss the need to extract coefficients from the polynomial expansions after applying the modulus operation.
  • Michael seeks clarification on obtaining coefficients for different prime values after performing the polynomial expansion.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper syntax in Mathematica and the approach to solving the polynomial expansion problem. However, there are differing opinions on the exact implementation details and the best methods to achieve the desired results.

Contextual Notes

Some participants express uncertainty about the syntax and functionality of certain Mathematica commands, indicating potential limitations in their understanding or the version of Mathematica they are using.

Who May Find This Useful

This discussion may be useful for Mathematica users looking for help with functional programming, polynomial expansions, and modular arithmetic, as well as those interested in learning resources for Mathematica.

mzu
Messages
9
Reaction score
0
Hi,

Quick question about Mathematica. As far as I know, squaring a negative number should give a positive result, right ? Let's say -2² should be 4 or is that wrong ? When doing this in Mathematica I get -4 as a result. Shouldn't it be 4 ?

Any help is greatly appreciated.

Also, does anybody know a good tutorial on functional programming in Mathematica ?

Thanks,
Michael
 
Physics news on Phys.org
OK I found it myself. Instead of just writing -2^2, I have to write (-2)^2 and this gives the correct result.
However, the question about a functional programming tutorial still stands.

Michael
 
Hi,

Personally I found the help/guides in Mathematica to be fairly sufficient to get me started. There is also lots of tutorials and seminars available online at wolfram.com:

http://www.wolfram.com/services/education/seminars/
http://demonstrations.wolfram.com/
http://www.wolfram.com/support/learn/
http://www.wolfram.com/mathematica/resources/

University websites tend also to be a good resource for Mathematica guides, often the computing support provide some help for beginners. A quick google search brings up one at:

http://www.cs.umd.edu/~atif/Teaching/Spring2007/Lectures/FunctionalProgramming.pdf

Or, if willing to spend some money then there are quite a few books available for Mathematica:

http://www.wolfram.com/books/

I have to say that I started many years ago with just the installed documentation and picked it up as I went along. It hasn't done me too much harm!

Good luck :-),

Ewan
 
Last edited by a moderator:
Hi,

Thanks for the info. I've been looking around on Wolframs website and looked at a few examples. I just got the book "An Introduction to Programming with Mathematica". This one seems quite good in explaining things.
I've done quite some calculations in Mathematica during the learning process and love it. This software is so powerful and I want to learn a lot more about it.
I've read that it should be possible to program Lego Mindstorms with Mathematica. Is this true ? Does anybody have more info on this ?

Thanks,
Michael
 
Hi all
Im tring to write a program statement using mathematica...
and please i need help.

How can i define P where P is Primenumbers between 2 and m^2.
where m = {1,2,...,200}
and i need to define m also.
please help.
 
76Ahmad said:
where m = {1,2,...,200}
and i need to define m also.

Not really sure what you want here. If m is a specific number, use:

Table[Prime[n], {n,m^2}]

If you want m to loop from 1 to 200, then just write a for loop and call Table[Prime[n], {n,m^2}] each time you cycle through.
 
Thank you for your help
what I'm trying to do is
I have this polynomial (1+x)^m where m is the set {2,3,...200}
and I want to Expand this polynomial for every m.
during that for every m expand reslt need to be Mod p
where p is Primenumbers between 2 and m^2

So its like a loop for example
if m =10
P will be the set {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}

and the expand of (1+x)^10 is

1+10x+45x^2+120x^3+210x^4+252x^5+210x^6+120x^7+45x^8+10x^9+x^10

and for that answer the coffeicent should be Mod p from 2 to 97 and give all ressult
......
so for that i need first to define two parameters m and p
please help
and thanks again
 
76Ahmad said:
Thank you for your help
what I'm trying to do is
I have this polynomial (1+x)^m where m is the set {2,3,...200}
and I want to Expand this polynomial for every m.
during that for every m expand reslt need to be Mod p
where p is Primenumbers between 2 and m^2

So its like a loop for example
if m =10
P will be the set {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}

and the expand of (1+x)^10 is

1+10x+45x^2+120x^3+210x^4+252x^5+210x^6+120x^7+45x^8+10x^9+x^10

and for that answer the coffeicent should be Mod p from 2 to 97 and give all ressult
......
so for that i need first to define two parameters m and p
please help
and thanks again

Do a nested for loop. Psudocode follows:

for (m = 2; m <= 200; m++)
for (p = 2; p <= m^2; p = next prime)

expand (1+x)^m (mod p)
print result

end
end

I haven't figured out how to tab on here.
 
i try to do it like you said::

For[m = 2, m ≤ 200, m++]
For[p = 2, p ≤ m^2, p = NextPrime]

i think the mathematica could not accept the word... p= NextPrime

then i write
Expand[(1+x)^m]Mod[p]

but i think some thing missing ?
 
  • #10
76Ahmad said:
i try to do it like you said::

For[m = 2, m ≤ 200, m++]
For[p = 2, p ≤ m^2, p = NextPrime]

i think the mathematica could not accept the word... p= NextPrime

then i write
Expand[(1+x)^m]Mod[p]

but i think some thing missing ?

I just gave you psudocode (not actual code, just an idea of what you want to do since I'm no mathematica guru). You want to figure out what the correct syntax is for mathematica.
 
  • #11
Thanks you are really help me so much...

i try to do loop by writing

Do[Print[Mod[Expand[(1 + x)^m], p]]]

i got good result but still not sure of it
 
  • #12
If you have an older version of Mathematica you must include this line, newer versions don't need it:

<<NumberTheory`NumberTheoryFunctions`

Either way then this will calculate your desired result

For[m=2,m≤200,m++,
For[p=2,p≤m^2,p=NextPrime[p],
Print[Mod[Expand[(1+x)^m],p]]
]
]

That will calculate your polynomial mod p.
But I'm not certain whether what you actually want is the coefficients of the polynomial mod p.
If that is what you want then perhaps this will do it.

For[m = 2, m ≤ 200, m++,
For[p = 2, p ≤ m^2, p = NextPrime[p],
Print[Expand[(1 + x)^m] /. Times[n_, v_] -> Times[Mod[n, p], v]
]
]
]
 
Last edited:
  • #13
Thanks Bill for your help, the last 4 days i was very ill , so sorry for my lait answer.

yes really what I'm looking for is the coefficients of the polynomial (1-x)^m when I must do these steps:

1- define m such that m goes from 2 to 200
2- define p such that p is a prime number goes from 2 to m^2
3- expand (1-x)^m and every expand i need to do PolynomialMod t p from 2 to m^2

for example: (when the program reach to m = 10)

In[1]:= Expand[(1-x)^10]

Out[1]= 1-10x+45x^2-120x^3+210x^4-252x^5+210x^6-120x^7+45x^8-10x^9+x^10

In[2]:= PolynomialMod[%, 2]

Out[2]= 1 + x^2 + x^8 + x^10 ...this is only mod (p = 2) for m = 10
and i also need (p=3,5,7) for the same m = 10
 
  • #14
after that i need to get all the coefficients that comes with every Mod operation to m=10

the last Coefficient when p=2 are: 1 0 1 0 0 0 0 0 1 0 1
what about p=3,5,7
 
  • #15
76Ahmad said:
Thanks Bill for your help, the last 4 days i was very ill , so sorry for my lait answer.

yes really what I'm looking for is the coefficients of the polynomial (1-x)^m when I must do these steps:

1- define m such that m goes from 2 to 200
2- define p such that p is a prime number goes from 2 to m^2
3- expand (1-x)^m and every expand i need to do PolynomialMod t p from 2 to m^2

for example: (when the program reach to m = 10)

In[1]:= Expand[(1-x)^10]

Out[1]= 1-10x+45x^2-120x^3+210x^4-252x^5+210x^6-120x^7+45x^8-10x^9+x^10

In[2]:= PolynomialMod[%, 2]

Out[2]= 1 + x^2 + x^8 + x^10 ...this is only mod (p = 2) for m = 10
and i also need (p=3,5,7) for the same m = 10

The code Bill Simpson listed above should do all of this.

76Ahmad said:
after that i need to get all the coefficients that comes with every Mod operation to m=10

the last Coefficient when p=2 are: 1 0 1 0 0 0 0 0 1 0 1
what about p=3,5,7

Try CoefficientList: http://reference.wolfram.com/mathematica/ref/CoefficientList.html
 
  • #16
If you need the results in some sort of expression you can later use for some other purpose, instead of just printing them, then perhaps this will help you

In[19]:=
maxm=5;
Reap[
For[m=2,m≤maxm,m++,
Map[
Sow[CoefficientList[PolynomialMod[Expand[(1+x)^m],#],x]]&,
Select[Range[2,m^2],PrimeQ]
]
]
][[2,1]]

Out[20]=
{{1,0,1},{1,2,1},{1,1,1,1},{1,0,0,1},{1,3,3,1},{1,3,3,1},{1,0,0,0,1},{1,1,0,1,1},{1,4,1,4,1},{1,4,6,4,1},{1,4,6,4,1},{1,4,6,4,1},{1,1,0,0,1,1},{1,2,1,1,2,1},{1,0,0,0,0,1},{1,5,3,3,5,1},{1,5,10,10,5,1},{1,5,10,10,5,1},{1,5,10,10,5,1},{
1,5,10,10,5,1},{1,5,10,10,5,1}}

Just change the maxm=5 to maxm=200 and wait

If that won't fit in memory or just printing them is enough then perhaps this will be more understandable

maxm = 5;
For[m = 2, m ≤ maxm, m++,
For[p = 2, p ≤ m^2, p++,
If[PrimeQ[p], Print[CoefficientList[PolynomialMod[Expand[(1 + x)^m], p], x]]]
]
]

but I think you should be prepared for a large result of about 305023 items
if I haven't made another mistake.
 
Last edited:
  • #17
Thanks[Bill_Simpson], Tnaks[gb7nash]
For[ your great help ]

i did run the program and it work 100%
and I'm so happppy :))
 
  • #18
One more thing please
during the run process if my laptop hang or shut down

how can i make the mathematica save my work and resume the
process from the point that it stopped from?
 
  • #20
Hi all again, new quistion about how to add the cofficients list?
here is an example:

For[m = 2, m ≤ 4 , m++,
For[p = 2, p ≤ m^2, p++,
If[PrimeQ[p], Print[CoefficientList[PolynomialMod[Expand[(1 - x)^m], p], x]]]]]

{1,0,1}
{1,1,1}

{1,1,1,1}
{1,0,0,2}
{1,2,3,4}
{1,4,3,6}

{1,0,0,0,1}
{1,2,0,2,1}
{1,1,1,1,1}
{1,3,6,3,1}
{1,7,6,7,1}
{1,9,6,9,1}


How to add every grup of cofficients together?
the reuslt should be like that:

{2,1,2}

{4,7,7,13}

{6,22,19,22,6}

please help :))
 
Last edited:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 20 ·
Replies
20
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
4
Views
2K