Frage

Ich habe ein Programm schreiben quadratics zu lösen, eine komplexe Zahl Ergebnis zurück.

Ich habe so weit gekommen, mit einer komplexen Zahl zu definieren, deklarieren sie Teil num, so +, zu sein - und *. - Ing stattfinden kann

Ich habe definiert auch einen Datentyp für eine quadratische Gleichung, aber im jetzt mit der eigentlichen Lösung der quadratischen stecken. Meine Mathe ist ziemlich schlecht, so dass jede Hilfe wäre sehr dankbar ...

data Complex = C {
re :: Float,
im :: Float
} deriving Eq

-- Display complex numbers in the normal way

instance Show Complex where
    show (C r i)
        | i == 0            = show r
        | r == 0            = show i++"i"
        | r < 0 && i < 0    = show r ++ " - "++ show (C 0 (i*(-1)))
        | r < 0 && i > 0    = show r ++ " + "++ show (C 0 i)
        | r > 0 && i < 0    = show r ++ " - "++ show (C 0 (i*(-1)))
        | r > 0 && i > 0    = show r ++ " + "++ show (C 0 i)


-- Define algebraic operations on complex numbers
instance Num Complex where
    fromInteger n       = C (fromInteger n) 0 -- tech reasons
    (C a b) + (C x y)   = C (a+x) (b+y)
    (C a b) * (C x y)   = C (a*x - b*y) (b*x + b*y)
    negate (C a b)      = C (-a) (-b)

instance Fractional Complex where
    fromRational r      = C (fromRational r) 0 -- tech reasons
    recip (C a b)       = C (a/((a^2)+(b^2))) (b/((a^2)+(b^2)))


root :: Complex -> Complex
root (C x y)
    | y == 0 && x == 0  = C 0 0
    | y == 0 && x > 0   = C (sqrt ( ( x + sqrt ( (x^2) + 0 ) ) / 2 ) )  0
    | otherwise         = C (sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ((y/(2*(sqrt ( ( x + sqrt ( (x^2) + (y^2) ) ) / 2 ) ) ) ) )


-- quadratic polynomial : a.x^2 + b.x + c
data Quad = Q {
    aCoeff, bCoeff, cCoeff :: Complex
    } deriving Eq


instance Show Quad where
    show (Q a b c) = show a ++ "x^2 + " ++ show b ++ "x + " ++ show c

solve :: Quad -> (Complex, Complex)
solve (Q a b c) = STUCK!

EDIT: Ich scheine den ganzen Punkt verpasst zu haben, meine eigene komplexe Zahl Datentyp zu verwenden, ist über benutzerdefinierte Datentypen zu lernen. Ich bin mir sehr wohl bewusst, dass ich complex.data nutzen könnte. Jede Hilfe, die gegeben werden könnte mit meinem Lösung so weit würde sehr geschätzt werden. \

EDIT 2: Es scheint, dass meine erste Frage schrecklich formuliert wurde. Ich bin mir bewusst, dass die quadratische Formel kehren beide (oder nur die eine) root zu mir. Wo Ich habe Probleme, wird diese Wurzeln als (komplexe, komplex) Tupel mit dem Code oben zurück.

Ich bin mir sehr wohl bewusst, dass ich die eingebaut quadratische Funktionen nutzen könnte, wie sie unten angezeigt worden, aber dies ist nicht die Übung. Die Idee hinter der Bewegung, und die eigene komplexe Zahl Datentyp zu schaffen, ist über benutzerdefinierte Datentypen zu erfahren.

War es hilfreich?

Lösung

Wie newacct sagte, es ist nur die quadratische Gleichung:

(-b +- sqrt(b^2 - 4ac)) / 2a
module QuadraticSolver where

import Data.Complex
data Quadratic a = Quadratic a a a deriving (Show, Eq)

roots :: (RealFloat a) => Quadratic a -> [ Complex a ]
roots (Quadratic a b c) = 
  if discriminant == 0 
  then [ numer / denom ]
  else [ (numer + root_discriminant) / denom,
         (numer - root_discriminant) / denom ]
  where discriminant = (b*b - 4*a*c)
        root_discriminant = if (discriminant < 0) 
                            then 0 :+ (sqrt $ -discriminant)
                            else (sqrt discriminant) :+ 0
        denom = 2*a :+ 0
        numer = (negate b) :+ 0

in der Praxis:

ghci> :l QuadraticSolver
Ok, modules loaded: QuadraticSolver.
ghci> roots (Quadratic 1 2 1)
[(-1.0) :+ 0.0]
ghci> roots (Quadratic 1 0 1)
[0.0 :+ 1.0,(-0.0) :+ (-1.0)]

Und die Anpassung Ihre Begriffe zu verwenden:

solve :: Quad -> (Complex, Complex)
solve (Q a b c) = ( sol (+), sol (-) )
  where sol op = (op (negate b) $ root $ b*b - 4*a*c) / (2 * a)

Obwohl ich diesen Code nicht getestet

Andere Tipps

Da Haskells sqrt auch komplexe Zahlen umgehen kann, rampion Lösung kann noch weiter vereinfacht werden:

import Data.Complex

-- roots for quadratic equations with complex coefficients
croots :: (RealFloat a) =>
          (Complex a) -> (Complex a) -> (Complex a) -> [Complex a]
croots a b c
      | disc == 0 = [solution (+)]
      | otherwise = [solution (+), solution (-)]
   where disc = b*b - 4*a*c
         solution plmi = plmi (-b) (sqrt disc) / (2*a)

-- roots for quadratic equations with real coefficients
roots :: (RealFloat a) => a -> a -> a -> [Complex a]
roots a b c = croots (a :+ 0) (b :+ 0) (c :+ 0)

Sie können auch diese croots Funktion mit Ihrem eigenen Datentyp verwenden, wenn Sie die Typen ändern Ihre Implementierung zu passen (und Ihre root Funktion statt sqrt nennen).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top