ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 22 Apr 2019 13:39:51 -0500Rounding, using modular arithmetic, etc. in find_fit?http://ask.sagemath.org/question/46299/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?Sun, 21 Apr 2019 07:20:34 -0500http://ask.sagemath.org/question/46299/rounding-using-modular-arithmetic-etc-in-find_fit/Comment by dan_fulea for <p>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:</p>
<pre><code>model(x,y) = (a*x+b*y+c)%(d*x+e*y+f)
</code></pre>
<p>However, I get the expected error:</p>
<pre><code>TypeError: unsupported operand parent(s) for %: 'Symbolic Ring' and 'Symbolic Ring'
</code></pre>
<p>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!</p>
<p>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:</p>
<pre><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
</code></pre>
<p>and the output is</p>
<pre><code>{d: 1.0, c: 1.0, b: 1.0, a: 1.0}
</code></pre>
<p>I would like it to return, say, </p>
<pre><code>{d: 2.0, c: 0.0, b: 1.0, a: 0.0}
</code></pre>
<p>Is there a way to make this work a bit better for my needs?</p>
http://ask.sagemath.org/question/46299/rounding-using-modular-arithmetic-etc-in-find_fit/?comment=46309#post-id-46309To avoid the immediate error one can introduce a true function like `model = lambda x, y, a, b, c, d, e, f: int(a*x + b*y + c) % int(d*x + e*y + f)`, but then it makes not to much sense to start a numerically fitting function against the model function. Which is actually the real need?Mon, 22 Apr 2019 13:39:51 -0500http://ask.sagemath.org/question/46299/rounding-using-modular-arithmetic-etc-in-find_fit/?comment=46309#post-id-46309