Komplexe Zahlen - Scheme |
22.11.2012, 14:59 | Auf diesen Beitrag antworten » |
robby_hd | Komplexe Zahlen - Scheme Meine Frage: Hallo liebe Leute, wir haben in der Uni folgende Aufgabe zu lösen: Implementieren Sie einen Verbund für komplexe Zahlen. Definieren Sie dazu einen Konstruktor make-complex (das erste Argument sollte der Realteil, das zweite Argument der Imaginärteil sein), Selektoren (complex-real k) und (complex-imag k) Implementieren Sie Operationen (complex+ k1 k2), (complex- k1 k2), (complex* k1 k2) und (complex-abs k), die komplexe Zahlen addieren, subtrahieren, multiplizieren bzw. deren Betrag berechnen. Leider steh ich vor dem Problem, dass ich damit noch überhaupt nicht richtig zurecht komme, habe bisher nur das Gerüst erstellt und ich bin mir noch nicht einmal bei den Signaturvereinbarungen sicher. Ich hoffe, dass hier jemand weiterhelfen kann :-/ Meine Ideen: ; Konstruktor make-complex erstellen (: make-complex (real imag -> complex)) (define make-complex (lambda)) ; Selektoren erstellen (: complex-real (real -> real)) (: complex-imag (imag -> imag)) (define complex-real (lambda (k))) (define complex-imag (lambda (k))) ; Operationen implementieren (: complex+ (real imag -> complex)) (: complex- (real imag -> complex)) (: complex* (real imag -> complex)) (: complex-abs (real -> real)) (define complex+ (lambda)) (define complex- (lambda)) (define complex* (lambda)) (define complex-abs (lambda)) |
|
|
23.11.2012, 13:08 | Auf diesen Beitrag antworten » |
Karlito | Hallo, ich kann leider kein Scheme und ich bräuchte sicher mehr als nur eine Stunde um mich entsprechend einzulesen. Deswegen kann ich Dir leider nicht helfen. Sobald Du eine Lösung hast, wäre ich Dir dankbar, wenn Du sie hier (möglichst kommentiert) postest. So kann auch ich etwas lernen. VG, Karlito |
23.11.2012, 21:50 | Auf diesen Beitrag antworten » |
robby_hd | Musterlösung ; Eine komplexe Zahl ist zusammengesetzt aus ; - einem Realteil (real) ; - einem Imaginärteil (real) (define-record-procedures complex make-complex complex? (complex-real complex-imag)) (: make-complex (real real -> complex)) (: complex? (any -> boolean)) (: complex-real (complex -> real)) (: complex-imag (complex -> real)) ; Addiert zwei komplexe Zahlen (: complex+ (complex complex -> complex)) (check-expect (complex+ (make-complex 0 0)(make-complex 0 0)) (make-complex 0 0)) (check-expect (complex+ (make-complex 2 4)(make-complex 1 5)) (make-complex 3 9)) (check-expect (complex+ (make-complex -2 3)(make-complex 4 -4)) (make-complex 2 -1)) (define complex+ (lambda (c1 c2) (make-complex (+ (complex-real c1) (complex-real c2)) (+ (complex-imag c1) (complex-imag c2))))) ; Subtrahiert zwei komplexe Zahlen (: complex- (complex complex -> complex)) (check-expect (complex- (make-complex 0 0)(make-complex 0 0)) (make-complex 0 0)) (check-expect (complex- (make-complex 2 4)(make-complex 1 5)) (make-complex 1 -1)) (check-expect (complex- (make-complex -2 3)(make-complex 4 -4)) (make-complex -6 7)) (define complex- (lambda (c1 c2) (complex+ c1 (make-complex (- 0 (complex-real c2)) (- 0 (complex-imag c2)))))) ; Multipliziert zwei komplexe Zahlen (: complex* (complex complex -> complex)) (check-expect (complex* (make-complex 0 0)(make-complex 0 0)) (make-complex 0 0)) (check-expect (complex* (make-complex 2 4)(make-complex 1 5)) (make-complex -18 14)) (check-expect (complex* (make-complex -2 3)(make-complex 4 -4)) (make-complex 4 20)) (define complex* (lambda (c1 c2) (make-complex (- (* (complex-real c1) (complex-real c2)) (* (complex-imag c1) (complex-imag c2))) (+ (* (complex-real c1) (complex-imag c2)) (* (complex-imag c1) (complex-real c2)))))) ; Berechnet den Betrag einer komplexen Zahl (: complex-abs (complex -> real)) (check-expect (complex-abs (make-complex 0 0)) 0) (check-expect (complex-abs (make-complex 3 4)) 5) (check-within (complex-abs (make-complex -2 2)) 2.8284 0.0001 ) (define complex-abs (lambda (c) (sqrt (+ (square (complex-real c)) (square (complex-imag c)))))) ;Berechnet das Quadrat einer Zahl (: square (number -> number)) (define square (lambda (x) (* x x))) |
23.11.2012, 22:05 | Auf diesen Beitrag antworten » |
Karlito | Danke für die Lösung. VG Karlito |
Anzeige | |
|
|