Python Forum
Programming Difficult math in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programming Difficult math in Python
#1
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
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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
Reply
#4
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'?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(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
Reply
#6
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'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,734 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  Math python question Deonvek 6 1,089 Apr-05-2023, 09:27 PM
Last Post: deanhystad
  difficult string conversion need help Pir8Radio 3 1,000 Nov-27-2022, 06:15 PM
Last Post: Gribouillis
  math.log versus math.log10 stevendaprano 10 2,314 May-23-2022, 08:59 PM
Last Post: jefsummers
  Math Package in python Uma 1 1,462 Dec-12-2021, 02:01 PM
Last Post: jefsummers
  Programming robots using Python OscarBoots 5 3,356 Oct-31-2021, 09:38 AM
Last Post: Larz60+
  creating simplex tableau pivot program easy or difficult for a beginner in Python? alex_0 2 2,538 Mar-31-2021, 03:39 AM
Last Post: Larz60+
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,707 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  "SyntaxError: invalid syntax" running code in Doing Math With Python b saucerdesigner 2 2,702 Nov-03-2020, 04:23 PM
Last Post: saucerdesigner
  How to solve difficult non-linear equations? shreeniket 3 2,348 Apr-23-2020, 01:36 AM
Last Post: shreeniket

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020