Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /trunk/src/compiler/IL/test-kernel.sml
ViewVC logotype

View of /trunk/src/compiler/IL/test-kernel.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (download) (annotate)
Fri Jul 9 21:38:01 2010 UTC (9 years, 1 month ago) by jhr
File size: 3442 byte(s)
  Added kernel printing code
(* test-kenel.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
 * All rights reserved.
 *
 * Text driver for the kernel code.
 *)

structure Test =
  struct

    structure R = Rational
    structure K = Kernel

    fun eval ({isOdd, isCont, segs}, x) = let
	  fun eval' x = let
		val {whole, frac} = Real.split x
		val i = Real.trunc whole
		fun evalPoly [] = 0.0
		  | evalPoly (c::r) = R.toReal c + x * evalPoly r
		in
		  evalPoly (List.nth(segs, i)) handle _ => 0.0
		end
	  in
	    if (x >= 0.0) then eval' x
	    else if isOdd then ~(eval' (~x))
	    else eval' (~x)
	  end


    local
    (* Path to Ploticus command *)
      val plPath = "/usr/local/bin/pl"
      val env = [
	      "PLOTICUS_PREFABS=/usr/local/src/ploticus/prefabs"
	    ];
    in
    fun ploticus args outFn = let
	  val proc = Unix.executeInEnv (plPath, args, env)
	  val outS = Unix.textOutstreamOf proc
	  in
	    outFn outS;
	    TextIO.closeOut outS;
	    Unix.reap proc
	  end
	      
    fun output mergedData outS = let
	  fun plotRow (x, l) = (
		TextIO.output(outS, Format.format "%f" [Format.REAL x]);
		List.app (fn t => TextIO.output(outS, Format.format " %f" [Format.REAL t])) l;
		TextIO.output(outS, "\n"))
	  in
	    List.app plotRow mergedData
	  end

    (* command-line arguments for ploticus *)
    fun args (file, name) = [
	    "-prefab", "lines",
	    "-eps",
	    "-o", file,
	    "-font", "/Times-Roman",
	    "-textsize", "12",
	    "data=-",
	    "rectangle= 0 1.0 5.5 5.5",
	    "legend= max-1 max",
	    "x=1",
	    "xlbl=X",
	    "xnearest=1",
	    "y=2",
	    "y2=3",
	    "y3=4",
	    "y4=5",
	    "ylbl=Y",
	    "ynearest=1",
	    "pointsym=none",
	    "pointsym2=none",
	    "pointsym3=none",
	    "pointsym4=none",
	    "name= D0",
	    "name2= D1",
	    "name3= D2",
	    "name4= D3",
	    "title= "^name
	  ];

    end;

  (* given a kernel, kernel and its derivatives *)
    fun plotKernel kern = let
	  val s = Kernel.support kern
	  val curve0 = Kernel.curve(kern, 0)
	  val curve1 = Kernel.curve(kern, 1)
	  val curve2 = Kernel.curve(kern, 2)
	  val curve3 = Kernel.curve(kern, 3)
	  val maxX = Real.fromInt s
	  val step = 1.0 / 64.0
	  fun lp (x, rows) = if (x <= maxX)
		then let
		  val rows = (x, [eval(curve0, x), eval(curve1, x), eval(curve2, x), eval(curve3, x)]) :: rows
		  in
		    lp (x+step, rows)
		  end
		else List.rev rows
	  val rows = lp (~maxX, [])
	  val name = K.name kern
	  in
	    ploticus (args (name ^ ".eps", name)) (output rows);
	    OS.Process.system (concat["/usr/bin/open ", name, ".eps"])
	  end

    fun polyToString poly = let
	  fun c2s c = Format.format "%g" [Format.REAL(R.toReal c)]
	  fun toS ([c], l) = c2s c :: l
	    | toS (c::r, l) = c2s c :: " + x*(" :: toS(r, ")"::l)
	  in
	    String.concat (toS (poly, []))
	  end

    fun printKernel (kern, k) = let
	  val {isCont, isOdd, segs} = K.curve(kern, k)
	  val name = if (k > 0)
		then concat(K.name kern::List.tabulate(k, fn _ => "'"))
		else K.name kern
	  fun lp (_, []) = ()
	    | lp (i, poly::rest) = (
		print(concat[
		    "  ", name, "(x) = ", polyToString poly, " for ",
		    Int.toString i, " <= x < ", Int.toString(i+1), "\n"
		  ]);
		lp (i+1, rest))
	  in
	    print(name ^ ":\n");
	    lp (0, segs);
	    if (isOdd)
	      then print(concat["  ", name, "(x) = -", name, "(-x) for x < 0\n"])
	      else print(concat["  ", name, "(x) = ", name, "(-x) for x < 0\n"])
	  end

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0