Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Rounding, using modular arithmetic, etc. in find_fit?

I'm learning how to use find_fit and it works great for polynomials, but I'd like to include expressions that require integers. My variables are all integers, and my coefficients are expected to be relatively simple rationals (i.e. small denominators). In particular, I'd like to use a model like:

model(x,y) = (ax+by+c)%(dx+ey+f)

However, I get the expected error:

TypeError: unsupported operand parent(s) for %: 'Symbolic Ring' and 'Symbolic Ring'

If I try to convert (dx+ey+f) to an integer within the model using int(), ceiling(), etc. then it won't convert, since of course it's a symbolic expression. Is there a way to round within a symbolic expression? Or any suggestions for other workarounds? Thanks!

Rounding, using modular arithmetic, etc. in find_fit?

I'm learning how to use find_fit and it works great for polynomials, but I'd like to include expressions that require integers. My variables are all integers, and my coefficients are expected to be relatively simple rationals (i.e. small denominators). In particular, I'd like to use a model like:

model(x,y) = (ax+by+c)%(dx+ey+f)

(a*x+b*y+c)%(d*x+e*y+f)

However, I get the expected error:

TypeError: unsupported operand parent(s) for %: 'Symbolic Ring' and 'Symbolic Ring'

Ring'

If I try to convert (dx+ey+f) to an integer within the model using int(), ceiling(), etc. then it won't convert, since of course it's a symbolic expression. Is there a way to round within a symbolic expression? Or any suggestions for other workarounds? Thanks!

Rounding, using modular arithmetic, etc. in find_fit?

I'm learning how to use find_fit and it works great for polynomials, but I'd like to include expressions that require integers. My variables are all integers, and my coefficients are expected to be relatively simple rationals (i.e. small denominators). In particular, I'd like to use a model like:

model(x,y) = (a*x+b*y+c)%(d*x+e*y+f)

However, I get the expected error:

TypeError: unsupported operand parent(s) for %: 'Symbolic Ring' and 'Symbolic Ring'

If I try to convert (dx+ey+f) to an integer within the model using int(), ceiling(), etc. then it won't convert, since of course it's a symbolic expression. Is there a way to round within a symbolic expression? Or any suggestions for other workarounds? Thanks!

edit: I was able to get it to run, but it doesn't provide an acceptable solution. I imagine it's due to how find_fit works with user-defined functions but I don't know enough about the inner-workings to work that out. Here is my code:

data = [(i,(i%2) ) for i in range(30)]
var('a, b, c, d, x')

def f(x, a, b, c, d): 
    return int(a+b*x)%int(c*x+d)

fit = find_fit(data, f, parameters = [a, b, c, d], variables = [x], solution_dict = True)
print fit

and the output is

{d: 1.0, c: 1.0, b: 1.0, a: 1.0}

I would like it to return, say,

{d: 2.0, c: 0.0, b: 1.0, a: 0.0}

Is there a way to make this work a bit better for my needs?