How to convert Math Formula to Python code?
Asked Answered
A

3

5

Are there any easy ways to convert mathematical formulas to Python code? Perhaps translators, web reference, specific book chapters, anything ~

For regular expressions there are programs such as Kodos and sites such as pythonregex.com, so I was hoping there would be something similar for formula notation and Python.

Angola answered 6/2, 2011 at 9:3 Comment(7)
What exactly would converting a mathematical formula to python code look like? What's "python code" for a mathematical formula?Particularity
It would, I suspect, depend on what you mean by "mathematical formula". Translating pure calculations (with some variables, posisbly) is, generally speaking, pretty straight-forward. A sum can be expressed as a for loop.Eirena
There are certain formulas which are not so easy to translate. Examples: \sum_{n=0}^\infty 1/n^2, \int_0^1 \sin x dx, \lim_{x\to 0} (e^x - 1)/x.Pifer
You may check sagemath.org and related sagenb.comQuagmire
@Particularity you had to of checked my examples of the programs that do the same for regular expressions ~ @Pifer yeah, that's why it was a long shot, some formulas (although not necessarily strictly mathematical as i had asked for) are not possible to translate as Paul points outAngola
@Mleep: Yes, but regular expressions are an integrated part of the language. So pythonregex is already in python. There's nothing to convert. What exactly is your expected input/output?Particularity
Take a look at this article. It may help: matt.might.net/articles/discrete-math-and-codeIntrogression
H
8

No, this isn't possible in general. There are mathematical functions that aren't computable (for example, see wikipedia/Halting_problem). There are other mathematical functions where it's just not obvious how to code them up (consider a difficult integral or differential equations). There are many books written on finding numerical solutions to these sorts of problems (you can find some links here: wikipedia/Numerical_analysis).

For simple cases, you can transcribe mathematical formulae directly, but any automated means of translation would require a formal language for writing mathematical formulae in what would be a programming language in itself. This would beg the question, since you would be trading writing mathematical formulae in one language with writing them in another.

Handbreadth answered 6/2, 2011 at 11:29 Comment(4)
makes sense, although regular expressions aren't special symbols, it could be argued that because they use regular grammars, they are a formal language. numerical analysis was going to be my next step; it's simple, straight forward, but busy work. was hoping there was a quicker solution. thanks for your reply!Angola
@Mleep: Numerical analysis is not "simple, straight forward", and it's not just "busy work". What math are you in? Is this something you're trying to do for a project or something?Particularity
@Particularity Well to some it's a fairly linear process. I'm working in level sets making 2d and 3d fluid simulations, the formulas are irritating to rewrite in python and was hoping for a translator. No need to go the "higher than thou" route here. When I know a formula, or it's already been done before, converting it is busy work. That's not an opinion.Angola
@Morvern: I wasn't trying to go the "higher than you" route. I was just curious as it makes a differences whether you're in first semester calc or writing your PhD thesis on differential equations in multidimensional hyperbolic geometry or something.Particularity
T
2

You could try to make your own with sympy and pyparsing.

Transcontinental answered 4/12, 2013 at 4:27 Comment(0)
D
0

so how to convert a math formula into python? well, i myself dont know pretty much of python (i am just a beginner) but i have got some tips to help you: first, you should like calculate the formula using x and y (or a and b) and write that down on a paper. lets say phythagoras theory ( which is kinda basic). c2 = a2 + b2; so you have to input those a and b and write this formula to calculate c (which will be c = sqrt a2 + b**2) now lets say you dont know this formulas (i can hear you). then? well, you should search it up on internet, listen to its explanation and try to transform them to python. this is like noobs advice, but you know - no easy ways in programming. (well, just search it on internet, i guess?)

Doodlesack answered 6/6, 2022 at 17:56 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Hauge

© 2022 - 2024 — McMap. All rights reserved.