[Tutor] Random equation generator

TexasJerky100 at aol.com TexasJerky100 at aol.com
Sat Dec 6 17:34:06 CET 2008


I think this maybe will work out.  I might have  to just go with the 
generalized form as you suggest
and forget about the random aspect.
 
 
 
 
 
In a message dated 12/6/2008 10:05:16 A.M. Central Standard Time,  
roadierich at googlemail.com writes:

Please  use the reply-all button when responding, so that your message gets 
sent to  the list as well.  


If that's the sort of equation you're after, than the easiest way would  
probably to decide on a generalised form:
 
y=(2x-1)*4x

y = 4*x**2 - 4x

y=2+5(x-1)

y = 5*x - 3

y=(2x+5)+(5x-25)




y = 10*x**2 + 75*x - 125 


y=((x+13)/((x-18))*(2x-1)

y  = (2*x**2 - 19*x -13) / (x - 18)


Except for the last equation, they are all of the "general form" ax^2 +bx  + 
c.


These can be generated using tools from the random module:
(pseudo python)
equation = randint(minimum, maximum) * x ** 2 + randint(minimum,maximum)  * x 
+ randint(minimum,maximum)


if you want to generate equations in the form of the last one you  mentioned, 
it will be a bit more difficult.


You could add a random number of terms with a random power, but actually  
impliementing it might be a bit harder:


import random
def generate_terms(minimum, maximum):
    "generate a list of random terms for an equation of  the form (a*x)**b + 
...  "
    return [(random.randint(minimum, maximum),  random.randint(-1,1)) for x 
in xrange(random.randint(1, 10))]
    
class Line(object):
    epsilon = 1e-15 
    def __init__(self, equation_terms):
        self._equation_terms =  equation_terms
    def is_on_line(self, (x,y)):
        calulated_y = sum((a * x) ** b for a,b  in self._equation_terms)
        return abs(y - calculated_y) <=  self.epsilon #see message from Paul


Then, instead of picking two points, you can do:


line = Line(generate_terms(-25, 25))


then proceed as with my previous message.


getting a nice string representation won't be as easy... unless you're  happy 
with seeing x**-1's dotted around.


You'll need to tweak the values within the generate_terms function to get  
exactly what you want.




Alternatively (and I'm probably going to get shouted at for suggesting  it), 
you could write something that generates random python equations, and use  
some variety of eval() to get a value out of it...  


Does that help?


On 5 Dec 2008, at 13:52, _TexasJerky100 at aol.com_ 
(mailto:TexasJerky100 at aol.com)  wrote:



Thanks for the response.   This will help me  passing the points and 
compairing the results.
I however still need to find something that will generate the actual  linear 
equations or similar equations in a
random fashion.  For instance lets say the program would first  generate the 
actual equations.
 
y=(2x-1)*4x
y=2+5(x-1)
y=(2x+5)+(5x-25)
y=((x+13)/((x-18))*(2x-1)
 
 
Then I could use your program to pass through the values.

 
 
Frank Hopkins
Houston, Tx
 
 
 
 
 
 
 
In a message dated 12/5/2008 7:29:42 A.M. Central Standard Time, 
_roadierich at googlemail.com_ (mailto:roadierich at googlemail.com)   writes:

When  you say linear, I'm assuming fitting y=mx+c, and passing through 
points?  


The line through points (x1, y1) and (x2,y2) is


y - y1 = (y2-y1) / (x2-x1) * (x-x1)


That multiplies out to:


y = (y2-y1)/(x2-x1) * x - (y2-y1)/(x2-x1) + y1
That gives m = (y2-y1)/(x2-x1) and c =  y1 -  (y2-y1)/(x2-x1)


you can then create a class to represent the line:


class Line(object):
    def __init__(self, (x1,y1), (x2,y2)):
        "create a line passing through  points (x1,y1) and (x2,y2) (inputted 
as tuples)"
        self.m = (y2-y1)/(x2-x1)
        self.c = y1 - self.m


def is_on_line(self,(x,y)):
        'Test if point represtented by an  (x,y) tuple is on the line"
        if self. m * x + self.c == y:
            return True
        else:
            return False


def __str__(self):
        "returns the equation of the line in  the form y=mx+c.  Might be 
quite long if floats are involved."
        print "y=%dx + %d" % (self.m,  self.c)




Then all you have to do is choose a pair of points, then iterate over  the 
list, testing each point in turn, wash, rinse and repeat. 


Does that help?
    

 
On 4 Dec 2008, at 20:28, _TexasJerky100 at aol.com_ 
(mailto:TexasJerky100 at aol.com)   wrote:



I am starting out with 7 fixed reference points.   From there I want  a 
program that can randomly
generate linear equations.  After the equations are generated  I would then 
like to randomly insert
the 7 fixed reference points into the equations and calculate the  results.  
I currently have several
programs that can generate random string of words from a file that  contains 
a list of word but is not
much help creating random equations.
  Do you know if there is such a program that can do  what  I am trying to 
get accomplished??
 
Thanks
 
Frank Hopkins
Houston, Tx



 
____________________________________
 Make your life easier with all your friends, email, and favorite sites  in 
one place. _Try  it  now_ 
(http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom00000010) .

_______________________________________________
Tutor  maillist  -  _Tutor at python.org_ (mailto:Tutor at python.org) 
_http://mail.python.org/mailman/listinfo/tutor_ 
(http://mail.python.org/mailman/listinfo/tutor) 





=




 
____________________________________
 Make your life easier with all your friends, email, and favorite sites in  
one place. _Try  it  now_ 
(http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom00000010) .





=

**************Make your life easier with all your friends, email, and 
favorite sites in one place.  Try it now. 
(http://www.aol.com/?optin=new-dp&icid=aolcom40vanity&ncid=emlcntaolcom00000010)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081206/881b5d8d/attachment-0001.htm>


More information about the Tutor mailing list