#๐ is there a way I can convert a mathematical function into a python function?
17 messages ยท Page 1 of 1 (latest)
@tulip needle
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
That's called a function. Here is the section from the python docs on defining functions. There are also a multitude of tutorials on the resources page that will cover making functions.
We're a large, friendly community focused around the Python programming language. Our community is open to those who wish to learn the language, as well as those looking to help others.
I am aware
I was thinking something like a meta function that takes a mathematical function as a parameter and makes it into a python function
or some python library that can interpret symbolic math maybe?
so I wouldn't have to reinvent the wheel
I'm not sure what you mean by that... the function you're talking about can be made as just a simple function that takes a single parameter x, and returns the computation of your expression
If you just mean calculating the result of degree two polynomials, something like ```py
def f(x, a, b, c):
return a*(x**2)+b*x+c
?
maybe I should share what I'm working on
here's my code
import numpy as np
from fractions import Fraction
sequence = [12565, 107338, 981331, 8924248, 80655313]
indices_to_test = list(range(5))
def get_coefficients(n):
return[1, 2**n, 3**n, n, 9**n]
coeff_array = np.array([get_coefficients(i) for i in indices_to_test])
sequence_array = np.array([sequence[i] for i in indices_to_test])
solutions = np.linalg.solve(coeff_array, sequence_array)
solutions_as_fraction = [Fraction(value).limit_denominator() for value in solutions]
print("Equation: ({}) + ({})2^n + ({})3^n + ({})n + ({})9^n".format(*solutions_as_fraction))
for i in range(20):
predicted_value = solutions_as_fraction[0] + solutions_as_fraction[1] * 2**i + solutions_as_fraction[2] * 3**i + solutions_as_fraction[3] * i + solutions_as_fraction[4] * 9**i
is_correct = (predicted_value == sequence[i] if i < len(sequence) else False)
print(f"index = {i}, predicted value = {predicted_value}, is correct? = {is_correct}")
I want some way so I wouldn't have to change the print statement any time I change the expressions
okay, that seems nothing like what you asked in your original post ๐
I know there is the sympy library... that can do stuff with symbols... but unsure if it's what you need
sorry, I just meant something where I can define an expression as 3^n so I can use it that way in the print statement yet python can still interpret it
okay, I will look into that thanks
I will try some things and ask again if I need help later
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.