As solving an equation is basically giving the set of its solutions, I
prefer to use a set to solve the equation.Instead of the OrederedCollection
of course. It simplifies the algorithm if the discriminant is 0, because
adding two times the same root yields a set with only one element, not the
same element counted twice. The algorithm (much like the one with ordered
collection) goes like this:

*create an empty set with Set new.
*compute the discriminant.
*if it is positive, add to the set the "two" roots.
*you're done!


On Wed, Apr 2, 2014 at 8:45 PM, nacho <0800na...@gmail.com> wrote:

>
> Hi Nacho,
>
> I wouldn't throw an error because otherwise one must know beforehand that
> an
> equation is non-resolvable or must use #on:do: everywhere.
> Since that when you ask to resolve an equation you get back a collection of
> solutions, a non-resolvable equation could just return an empty collection,
> couldn't it?.
> If you really want to consider that asking to resolve a non-resolvable
> equation is an error, another solution would be to provide a #rootsIfNone:
> method. That's a common pattern in Smalltalk.
> I would also rename 'checkNegative' to 'discriminant', because it's not
> obvious to see what that variable represents at first glance.
>
> Very good points. I came up with a third draft. This is evolving good!
> Thanks
> Nacho
>
> "The idea is to have a class to calculate quadratic equations.
> This second third still only calculates real roots (leaving aside imaginary
> roots).
> But thanks to Damien Cassou, and Camille Teruel has a better style.
>
> A QuadraticEquation is a class used to solve Quadratic Equations of the
> form: ax^2 + bx + c = 0, where a is not 0.
>
> You can set the coefficients using the following method
> setQuadraticCoefficient:  linearCoefficient:  constant:"
>
> Object subclass: #QuadraticEquation
>         instanceVariableNames: 'quadraticCoefficient linearCoefficient
> constant
> roots'
>         classVariableNames: ''
>         category: 'IS-Math'
>
> "I create accessors (only getters for each term) and then the following
> method to set the terms"
> setQuadraticCoefficient: aNumber1 linearCoefficient: aNumber2 constant:
> aNumber3
>         quadraticCoefficient := aNumber1.
>         linearCoefficient := aNumber2.
>         constant := aNumber3
>
>
>
> QuadraticEquation>>calculateRoots
>         | discriminant |
>          discriminant := linearCoefficient  squared - ( 4 *
> quadraticCoefficient  *
> constant).
>
>          discriminant  >= 0
>                                 ifFalse: [ ^ self rootsIfNone ]
>                                 ifTrue: [ ^ self solveRoots: discriminant]
>
>
> QuadraticEquation>>solveRoots: aTerm
>         | rootA rootB |
>         roots := OrderedCollection new.
>         rootA := (linearCoefficient  negated + aTerm sqrt) / (2 *
> quadraticCoefficient).
>         rootB := (linearCoefficient  negated  - aTerm sqrt) / (2 *
> quadraticCoefficient).
>         roots add: rootA; add: rootB.
>         ^ roots
>
>
> QuadraticEquation>>rootsIfNone
>         ^ 'No real solution for the equation'
>
>
> "       Example:  -3x^2+2x+2=0
>         an OrderedCollection(-0.5485837703548636 1.2152504370215302)
>
>         print(it)
>
>         | anEquation |
>         anEquation := QuadraticEquation new.
>         anEquation setQuadraticCoefficient: -3 linearCoefficient: 2
> constant: 2.
>         anEquation calculateRoots."
>
>
>
>
>
>
> -----
> Nacho
> Smalltalker apprentice.
> Buenos Aires, Argentina.
> --
> View this message in context:
> http://forum.world.st/Question-on-style-tp4752165p4752369.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>
>

Reply via email to