Programmiersprachen (Re: [linux-l] Ruby: sehr cool, aber laaaahm... wie geht's schneller?! - D?)

Oliver Bandel oliver at first.in-berlin.de
So Aug 27 14:13:52 CEST 2006


Hi,


nun hatte ich ja ganz vergessen,
die derive-Funktion anzupassen und die beidseitig vom X-Wert
berechnen zu lassen...

...hmhhh, nun denn also am besten vorsehen, daß man mehrere
Implementationen hat.

Also ein Modul für jede Implementation, und dann das
Modul, das man letztlich für das bessere hält dann auswählen.

Könnten ja zwei Programmierer sein, die je eine Implementation
bringen, also gibt man das Interface vor.
Der bessere Code wird dann später fest einkompiliert.
<Ketzer>
   (Der schlechtere für die kostenlose Demo-Version? ;-))
</Ketzer>


Die Signatur soll so aussehen:

module Derive :
  sig val derive : (float -> float) -> float -> float -> float end

Wobei der Name "Derive" reserviert ist für das Modul, das ausgewählt wird.

Nennen wir die beiden Module also zum Beispiel SymDerive und AsymDerive;
später könnte man auch RightDerive und LeftDerive und SymDerive nehmen
oder sowas.


Der Code sähe dann so aus in OCaml:

=================================================================

module AsymDerive =
  struct
    (* -------------------------------------------------------------------- *)
    (* This function is for calculating the numerical derivation for a      *)
    (* function  f  with lower x-value at  x  and higher x-value at         *)
    (* x + epsilon.                                                         *)
    (* -------------------------------------------------------------------- *)
    let derive f x epsilon = (  f(x +. epsilon) -. f x ) /. epsilon
  end

module SymDerive =
  struct
    (* -------------------------------------------------------------------- *)
    (* This function is for calculating the numerical derivation for a      *)
    (* function  f  symmetrically around x, with two values, epsilon / 2    *)
    (* around the x-value.                                                  *)
    (* -------------------------------------------------------------------- *)
    let derive f x epsilon =
       let heps = epsilon /. 2.0 in
         ( f(x +. heps) -. f (x -. heps) ) /. epsilon
  end


module Derive = SymDerive

(* --------------------------------------------------------------- *)
(* This function calculates the derivative with an epsilon = 0.001 *)
(* --------------------------------------------------------------- *)
let myderive f x = Derive.derive f x 0.001


(* -------------------------------------------------------------------- *)
(* List of pair, where the pairs are of type ( float -> float, string ) *)
(* This will be feed into the calculating function.                     *)
(* -------------------------------------------------------------------- *)
let function_list_with_titel = [
    (sin, "Sinus");
    (cos, "Kosinus");
    ((fun x -> x *. x), "Quadrat (Parabel)");
    ((fun x -> x *. x *. x), "Kubik") ]


(* -------------------------------------------------------------------- *)
(* The calculation and printout will be done here.                      *)
(* This function needs the (function,title)-pair, the x_start and x_end *)
(* values and the number of intervalls (steps).                         *)
(* The true intervall-size will be calculated.                          *)
(* -------------------------------------------------------------------- *)
let calc_and_print_values fwt x_start x_end steps =
  let sort_pair (x,y) = if x > y then (x,y) else (y,x) in
  let func     = fst fwt
  and title    = snd fwt
  and (xe,xs)  = sort_pair (x_start, x_end) in (* Achtung: xe soll > xs sein! :) *)
  let truestep = (xe -. xs) /. float_of_int (abs steps) in

    Printf.printf "\n\nBerechnung von %s:\n" title;
    for index = 0 to steps
      do
        let x = xs +. (float_of_int index) *. truestep in
        let y = func x in
        let y' = myderive func x in
        Printf.printf "x: %f, y: %f, y': %f\n" x y y'
      done



let _ =
      (* For the list of functions/titles do all the calculations *)
      List.iter (fun pair -> calc_and_print_values pair 0.0 6.28 63) function_list_with_titel

=================================================================


Ach ja, die Signatur für's ganze:


=================================================================
first:~/Programmierung/codebeispiele oliver$ ocamlc -i numabl.ml
module AsymDerive :
  sig val derive : (float -> float) -> float -> float -> float end
module SymDerive :
  sig val derive : (float -> float) -> float -> float -> float end
module Derive :
  sig val derive : (float -> float) -> float -> float -> float end
val myderive : (float -> float) -> float -> float
val function_list_with_titel : ((float -> float) * string) list
val calc_and_print_values :
  (float -> float) * string -> float -> float -> int -> unit
first:~/Programmierung/codebeispiele oliver$ 
=================================================================

Vorschläge aus an deren Sprachen? :)


Gruß,
   Oliver



Mehr Informationen über die Mailingliste linux-l