From: Daniel Moore Date: 2009-02-09T05:33:44+09:00 Subject: Re: Mathematical Image Generator (#191) I'm still not doing a very good job of explaining this. One more attempt, this time by a coding example. CAUTION: Spoiler! If you want to generate random functions on your own do not read further. This implements the function generation, not anything specific to image generation. Here is a class that will generate string representations of functions given a specified depth. Feel free to use or modify this class in your solutions. class FunctionMaker def initialize() @functions = ["Math.sin(Math::PI * ?)", "Math.cos(Math::PI * ?)", "(? + ?)/2", "? * ?", "(?) ** 3"] end # Create a string representation of a function at the given def create_function(depth) #if depth is 0, return either x or y randomly if depth == 0 return "x" if rand > 0.5 return "y" end #grab a random function from the list function = @functions[rand(@functions.length)] #for each instance of ? in the function, replace with a function at the next shallower depth return function.gsub("?") { create_function(depth - 1) } end end Usage: fm = FunctionMaker.new fm.create_function(5) => "(Math.sin(Math::PI * ((Math.cos(Math::PI * y) + y * y)/2 + y * x * y * y)/2)) ** 3" On Sat, Feb 7, 2009 at 9:21 PM, Sean Mehan wrote: > And is this before or after collection? > > 2x + x + 5x = 8x, has one term of x at degree 1. > > 2x^2 -x + 4x^2 + x = 6x^2, has one term of x at degree 2 and none at > degree 1.... > > > >> Di Mo wrote: >>> My explanation of function depth is also somewhat confusing. If anyone >>> sees what I'm getting at and wants to try and help me out with another >>> explanation please do! Let me try again in the meantime: >> >> Does "depth" equal "the number of times 'x' (or 'y') appears in the >> expression" ?