Mathematica Help for Negative Numbers and Functional Programming

  • Mathematica
  • Thread starter mzu
  • Start date
  • Tags
    Mathematica
In summary: I'm looking for all mods(p) for m = 2 to 200...so I'm waiting for your help, and thanks again for your helpIn summary, the conversation discusses using Mathematica to square negative numbers and expands on the topic of functional programming in Mathematica. It also mentions resources for learning Mathematica, such as tutorials and books. The conversation then shifts to defining and expanding a polynomial with varying parameters, specifically using the function PolynomialMod to find the coefficients of the polynomial mod p. The conversation ends with a request for help with implementing this process in Mathematica.
  • #1
mzu
10
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
  • #2
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
 
  • #3
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:
  • #4
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
 
  • #5
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.
 
  • #6
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.
 
  • #7
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
 
  • #8
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.
 
  • #9
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:

What is the purpose of using negative numbers in functional programming?

Negative numbers in functional programming are used to represent values that are less than zero. This is important for certain mathematical operations and can also be used to indicate the direction of a vector.

How do you handle negative numbers in functional programming?

Negative numbers can be handled in functional programming using built-in functions or by defining custom functions. For example, the built-in function "Abs" can be used to return the absolute value of a number, effectively converting negative numbers to positive numbers.

Can negative numbers be used as inputs in functional programming?

Yes, negative numbers can be used as inputs in functional programming. They can be passed as arguments to functions and used in mathematical operations just like positive numbers.

What are some common errors when working with negative numbers in functional programming?

Some common errors when working with negative numbers in functional programming include forgetting to handle negative numbers in a function, using the wrong operator for a mathematical operation, and not checking for potential negative results in a calculation.

Are there any special considerations for using negative numbers in functional programming?

One special consideration for using negative numbers in functional programming is to be aware of how they can affect the output of a function. For example, using a negative number as an index in a list may result in unexpected behavior if the list is not properly handled. It is important to carefully test and debug code that involves negative numbers to ensure correct functionality.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
940
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
20
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • Science and Math Textbooks
Replies
2
Views
1K
Back
Top