From: Benoit Daloze Date: 2010-05-14T03:13:51+09:00 Subject: Re: In Ruby, can the coerce() method know what operator it is th On 13 May 2010 17:43:24 UTC+2, Colin Bartlett wrote: > I wish I'd known about this >  http://github.com/eregon/math/blob/master/math/polynomial.rb > earlier this year: including differentiation _and_ integration! > I was using polynomials to form weighted sums of discrete and > continuous actuarial functions, so I needed integration of the > polynomials. You're very welcome to fork and use my code :) > I couldn't find a polynomial.evaluate method, but polynomial.run(n) > seems to give the numerical value of the polynomial for x == n: is > that right? Yes, #evaluate is bit long for a simple method like that. Note it's also aliased to #& for very lazy people like me. So Polynomial.new(...) & 4 > ### def -@(); @coef.map! { |c| -c }; end > q = Polynomial.new( 2, 3, 1 ) > p q #=>   2x^2 + 3x + 1 > -q > p q #=> - 2x^2 - 3x - 1 > > If you don't mind me asking, what was the reason for #- changing the > polynomial to its negative as opposed to returning a new polynomial > which is the negative of the original polynomial? Thanks for pointing me out, is just some residual from a long time, when I didn't know yet about how ruby objects are never duplicated. I supposed I simplified it later, without seeing this error. (and I never used -P(x)) So now that's updated :) I'm also working on symbolic, if you're interested in this kind of maths. Regards, B.D.