Python Forum
Programming Difficult math in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Programming Difficult math in Python (/thread-21840.html)



Programming Difficult math in Python - Huntern - Oct-17-2019

Greetings, I am currently in Electrical Engineering school. Unfortunately Python is not used/taught in my school. I was only taught some basic C and microcontroller language. Basically, I am working on a unique personal project for my own use. I want to build a program that accepts user defined equations. My short term goals are to calculate derivatives and single integrals of functions. I would like to eventually expand my code to include second derivatives, triple integrals, and LaPlace transformations. I guess my overall goal is to have a far reaching program that can handle these types of advanced math operations and output me a result. One problem specifically that i am encountering is: i cannot figure out how to perform math operands on a char. For starters,I want to do the derivative of x^3 [ which= 3x^2]. It will not perform the typical math operands because the input is not an int. Will anyone please kindly provide some useful resources that may at all pertain to my program goals? Or even some example code of anything at all minutely related. Honestly, i will be very grateful for any resources anyone can provide to me. Like i said, i am a beginner so as dumbed-down- as can be is definitely appreciated.


Thanks- Hunter


RE: Programming Difficult math in Python - perfringo - Oct-17-2019

I don't understand exactly what is the problem you try to solve but maybe examples below can help:

>>> x = 5
>>> (3 * x) ** 2
225
>>> def my_func(x):
...     return (3 * x) ** 2
... 
>>> my_func(5)
225



RE: Programming Difficult math in Python - Huntern - Oct-17-2019

(Oct-17-2019, 05:50 AM)perfringo Wrote: I don't understand exactly what is the problem you try to solve but maybe examples below can help:
 >>> x = 5 >>> (3 * x) ** 2 225 >>> def my_func(x): ... return (3 * x) ** 2 ... >>> my_func(5) 225 
Thanks for the response! So one problem I am having is I want x to remain a variable. I do not want to assign an integer as x. I want to perform math on x as a Variable(character?).
For example I want to input:
equation =4x^5
Derivative= 20x^4.
I know how to perform this math on paper. I am also familiar with logic but I do not see how it could be used for this problem specifically. Long story short, I need to perform math on characters rather than ints


RE: Programming Difficult math in Python - perfringo - Oct-17-2019

Should it convert automatically from one formula to another? If not you can write a function and keep x a variable:

>>> def first_iteration(x):
...     return (4 * x) ** 5
...
>>> def second_iteration(x):
...     return (20 * x) ** 4
... 
>>> num = 5
>>> first_iteration(num)
3200000
>>> second_iteration(num)
100000000
>>>
What do you mean by 'math on characters'?


RE: Programming Difficult math in Python - Huntern - Oct-17-2019

(Oct-17-2019, 06:12 AM)perfringo Wrote: Should it convert automatically from one formula to another? If not you can write a function and keep x a variable:
 >>> def first_iteration(x): ... return (4 * x) ** 5 ... >>> def second_iteration(x): ... return (20 * x) ** 4 ... >>> num = 5 >>> first_iteration(num) 3200000 >>> second_iteration(num) 100000000 >>> 
What do you mean by 'math on characters'?
Example:
Input = 2ln(x)
//python math to perform derivative of 2*ln(x)
Output = 2/x
Problem is, when specifying variables (typically x, y, z, r, theta ,phi) they need to remain variables- or characters in programming?) the typical math operations within coding only apply to INT variables - not char/string. I need some algorithm/ understanding of how to apply mathematical operands on characters. Sorry if this does not make sense, it is somewhat difficult to discuss programming math.
Cheers-Huntef


RE: Programming Difficult math in Python - perfringo - Oct-17-2019

I would approach it from another angle. So you have output '2/x'. What do you intend to do with it?

There is Python library for symbolic math SymPy.


RE: Programming Difficult math in Python - Huntern - Oct-17-2019

(Oct-17-2019, 06:28 AM)perfringo Wrote: I would approach it from another angle. So you have output '2/x'. What do you intend to do with it? There is Python library for symbolic math SymPy.
I think the library you attached may be exactly what I am looking for in this particular step! So in this case, the output, 2/x which is the derivative of the user input equation would be my answer! Basically, I’m trying to write a program that performs calculus.Thank you so much I will begin looking into the sympi library immediately