func Dump() { bug.Where() n := 1 << Depth for i := 0; i < n; i++ { Leaves[i].Dump(0) } }
func commandHandler(w http.ResponseWriter, r *http.Request) { bug.Pr("Method", r.Method) bug.Pr("URL", r.URL) label := r.URL.Path[1:] bug.Pr("command", label) cmd, ok := command[label] if ok { bug.Where() http.Redirect(w, r, "/", http.StatusFound) fmt.Fprintf(w, doc()) cmd.fn(label, cmd) } else { bug.Where() fmt.Fprintf(w, doc()) } }
// Build splines based on quadratics // Does curve fitting witih quadratics for the form: // a*x*x + b*x + c = y // 1. First Segment - (x0, y0),(x1, y1) // a. Assume a == 0 // b. Set up the first two points // c. Compute first derivative (slope) y'[1] // d. Plot (will be a line) // 2. For points i = 2; i < n; i++ // a. Use known derivative from previous step // b. a*x[i-1]*x[i-1] + b*x[i-1] + c = y[i-1] // c. a*x[i]*x[i] + b*x[i] + c = y[i] // d. 2*a*x[i-1] + b = y'[i-1] // e. Solve equations. // f. y'[i] ;= 2*a*x[i] + b // g. plot func (d *Doodle) GraphQuadSpline(list List) { bug.Where() n := len(list) m := make(Matrix, 3) bug.Pd("n", n) m[0] = FillPoly(list[0], 3) m[1] = FillPoly(list[1], 3) m[2] = FirstSlope() fmt.Printf("m=%v\n", m) v := m.GaussianElimination() fmt.Printf("m=%v\n", m) fmt.Printf("v=%v\n", v) d.Plot(func(x float64) float64 { return Poly(v, x) }, list[0].X, list[1].X) for i := 2; i < n; i++ { m[0] = FillPoly(list[i-1], 3) m[1] = FillPoly(list[i], 3) m[2] = Derivative(list[i-1].X, v[0], v[1]) fmt.Printf("m=%v\n", m) v = m.GaussianElimination() fmt.Printf("m=%v\n", m) fmt.Printf("v=%v\n", v) d.Plot(func(x float64) float64 { return Poly(v, x) }, list[i-1].X, list[i].X) } }
// Build splines based on cubic equations - attempt A // The four equations are: // 1. cubic(x-1) // 2. cubic(x) // 3. cubic(x+1) // 4. 1st derivative func (d *Doodle) GraphCubicSplineA(list List) { bug.Where() n := len(list) m := make(Matrix, 4) bug.Pd("n", n) m[0] = FillPoly(list[0], 4) m[1] = FillPoly(list[1], 4) m[2] = FillPoly(list[2], 4) m[3] = FillPoly(list[3], 4) fmt.Printf("m=%v\n", m) v := m.GaussianElimination() fmt.Printf("m=%v\n", m) fmt.Printf("v=%v\n", v) d.Plot(func(x float64) float64 { return Poly(v, x) }, list[0].X, list[1].X) for i := 2; i < n-1; i++ { m[0] = FillPoly(list[i-1], 4) m[1] = FillPoly(list[i], 4) m[2] = FillPoly(list[i+1], 4) m[3] = FirstDerivativeA(list[i-1].X, v[0], v[1], v[2]) //m[2] = FirstDerivative(list[i-1].X, v[0], v[1], v[2]) //m[3] = SecondDerivative(list[i-1].X, v[0], v[1]) fmt.Printf("m=%v\n", m) v = m.GaussianElimination() fmt.Printf("m=%v\n", m) fmt.Printf("v=%v\n", v) d.Plot(func(x float64) float64 { return Poly(v, x) }, list[i-1].X, list[i].X) } }
func (leaf *Leaf) Dump(indent int) { bug.Where() indent++ for i := 0; i < leaf.n; i++ { fmt.Print(string(Spaces[:2*indent]), i, ". ") fmt.Println(leaf.rec[i].key, leaf.rec[i].val) } }
func (b *Branch) Insert(key int, val int) { bug.Where() node := b.node[b.index(key)] if node.IsFull() { bug.Note("Node is full") } node.Insert(key, val) }
func (b *Branch) Dump(indent int) { bug.Where() indent++ for i := 0; i < b.n; i++ { fmt.Print(string(Spaces[:2*indent]), i, ". ") b.node[i].Dump(indent) } }
func (leaf *Leaf) Insert(key int, val int) { bug.Where() if leaf.n == maxRecords { fmt.Println("Needed to split") } else { leaf.rec[leaf.n].key = key leaf.rec[leaf.n].val = val leaf.n++ } }
func (leaf *Leaf) Split(bit uint) *Leaf { bug.Where() mask := 1 << bit var newLeaf Leaf for i := 0; i < leaf.n; i++ { if leaf.rec[i].key&mask != 0 { newLeaf.rec[newLeaf.n] = leaf.rec[i] newLeaf.n++ leaf.n-- leaf.rec[i] = leaf.rec[leaf.n] } } return &newLeaf }
func main() { flag.Parse() bug.Where() d := doodle.New(512, 512, -3, -3, 6, 6) d.Clear() if false { testSpline(d) } else { testTurtle(d) } paper = d.Paper http.HandleFunc("/", img) http.ListenAndServe(":62834", nil) }
func Insert(key int, val int) { bug.Where() if Root == nil { var leaf Leaf Root = &leaf } if Root.IsFull() { var b Branch b.node[0] = Root b.n++ b.depth = Root.Depth() + 1 Root = &b } Root.Insert(key, val) }
func Insert(key int, val int) { bug.Where() mask := ((1 << Depth) - 1) i := (key >> (maxLeavesLb - Depth - 1)) & mask bug.Px("mask", mask) leaf := Leaves[i] if leaf == nil { leaf = &Leaf{} Leaves[i] = leaf } if leaf.n < maxRecords { leaf.rec[leaf.n] = Record{key, val} leaf.n++ } else { } }
func runList(label string, cmd *Command) { bug.Where() if commandRunning { bug.Note("Command is running") toggleRunningColor() return } commandRunning = true defer clear(&commandRunning) cmd.state = running defer setIdle(cmd) for _, arg := range cmd.args { err := run(arg) if err != nil { return } } }
func goCmd(label string, cmd *Command) { bug.Where() if goRunning { bug.Note("go is running") toggleRunningColor() return } goRunning = true defer clear(&goRunning) cmd.state = running defer setIdle(cmd) for _, arg := range cmd.args { if goDebug { if run("go "+arg+" -debug") != nil { return } } else { if run("go "+arg) != nil { return } } } }
func testSpline(d *doodle.Doodle) { list := listA if false { d.Plot(quad, -2, 5) d.Plot(func(x float64) float64 { return doodle.Poly(doodle.Vector{1, 0, -5, 0, 4}, x) }, -2, 5) d.Plot(cubic, -2, 3) d.Draw(line, 1.4, 3) d.Plot(line, 1.4, 3) for z := 0.0; z < 2; z += 0.1 { d.Line(doodle.Pt(-1, -2), doodle.Pt(4, 4+z)) } d.GraphLine(list) d.GraphPoly(list) d.GraphQuadSpline(list) d.GraphCubicSplineA(list) d.GraphCubicSplineB(list) d.GraphCubicSplineC(list) } bug.Where() d.GraphLine(list) d.GraphCubicSplineC(list) }
func testTurtle(d *doodle.Doodle) { var n int var x, dx, a, da float64 bug.Where() t := d.New() n, x, dx, a, da = 400, 0.1, 0, 37, 17 if flag.NArg() == 5 { n, _ = strconv.Atoi(flag.Arg(0)) x, _ = strconv.ParseFloat(flag.Arg(1), 64) dx, _ = strconv.ParseFloat(flag.Arg(2), 64) a, _ = strconv.ParseFloat(flag.Arg(3), 64) da, _ = strconv.ParseFloat(flag.Arg(4), 64) } println(n, x, dx, a, da) if false { for i := 0; i < 4; i++ { t.Forward(2) t.Right(90) } t.Doit(40, 0, .1, 90, 0) } else { t.Doit(n, x, dx, a, da) } }
func (leaf *Leaf) IsFull() bool { bug.Where() return leaf.n == maxRecords }
func (b *Branch) IsFull() bool { bug.Where() return b.n == maxBranches }
func Dump() { bug.Where() Root.Dump(0) }
func Ktop(reportChan chan chan Callers) { bug.Where() StartCollector(reportChan) }
func NumSysCalls() uint64 { bug.Where() delta := NumEvents - OldNumEvents OldNumEvents += delta return delta }