|
|
|
@ -115,18 +115,90 @@ func TestNewton1(t *testing.T) { |
|
|
|
|
err = module.Run() |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
|
|
calculateNewton := func(start float64) float64 { |
|
|
|
|
f1 := func(x float64) float64 { |
|
|
|
|
return math.Pow(x, 5) + 5*math.Pow(x, 3) - 5 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
f1derived := func(x float64) float64 { |
|
|
|
|
return 5*math.Pow(x, 4) + 15*math.Pow(x, 2) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var last float64 |
|
|
|
|
x := start |
|
|
|
|
for x != last { |
|
|
|
|
last = x |
|
|
|
|
x = x - f1(x)/f1derived(x) |
|
|
|
|
} |
|
|
|
|
return x |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
assert.Equal(t, calculateNewton(1), module.IO[1]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func f1(x float64) float64 { |
|
|
|
|
return math.Pow(x, 5) + 5*math.Pow(x, 3) - 5 |
|
|
|
|
func TestNewton2(t *testing.T) { |
|
|
|
|
input := []string{ |
|
|
|
|
"01 START", |
|
|
|
|
"02 LOADNUM 1", |
|
|
|
|
"03 STORE 10", |
|
|
|
|
|
|
|
|
|
"10 MUL 10", |
|
|
|
|
"11 STORE 11", |
|
|
|
|
"12 MUL 10", |
|
|
|
|
"13 MUL 10", |
|
|
|
|
"14 MUL 10", |
|
|
|
|
"15 STORE 20", |
|
|
|
|
"16 MUL 10", |
|
|
|
|
"17 MULNUM 8", |
|
|
|
|
"18 STORE 12", |
|
|
|
|
"19 LOAD 11", |
|
|
|
|
"20 MULNUM 3", |
|
|
|
|
"21 ADD 12", |
|
|
|
|
"22 SUBNUM 3", |
|
|
|
|
"23 STORE 15", |
|
|
|
|
|
|
|
|
|
"24 SUB 50", |
|
|
|
|
"25 JUMPNULL 45", |
|
|
|
|
"26 LOAD 15", |
|
|
|
|
"27 STORE 50", |
|
|
|
|
|
|
|
|
|
"30 LOAD 20", |
|
|
|
|
"31 MULNUM 48", |
|
|
|
|
"32 STORE 21", |
|
|
|
|
"33 LOAD 10", |
|
|
|
|
"34 MULNUM 6", |
|
|
|
|
"35 ADD 21", |
|
|
|
|
"36 STORE 25", |
|
|
|
|
|
|
|
|
|
"40 LOAD 15", |
|
|
|
|
"41 DIV 25", |
|
|
|
|
"42 MULNUM -1", |
|
|
|
|
"43 ADD 10", |
|
|
|
|
"44 JUMP 03", |
|
|
|
|
|
|
|
|
|
"45 LOAD 10", |
|
|
|
|
"46 OUT 1", |
|
|
|
|
"47 STOP", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func f1derived(x float64) float64 { |
|
|
|
|
return 5*math.Pow(x, 4) + 15*math.Pow(x, 2) |
|
|
|
|
program, err := parser.ParseProgram(input) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
|
|
module, err := hal.NewHALModule(program, 256, 2, false) |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
|
|
err = module.Run() |
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
|
|
calculateNewton := func(start float64) float64 { |
|
|
|
|
f1 := func(x float64) float64 { |
|
|
|
|
return 8*math.Pow(x, 6) + 3*math.Pow(x, 2) - 3 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
f1derived := func(x float64) float64 { |
|
|
|
|
return 48*math.Pow(x, 5) + 6*x |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func calculateNewton(start float64) float64 { |
|
|
|
|
var last float64 |
|
|
|
|
x := start |
|
|
|
|
for x != last { |
|
|
|
@ -135,3 +207,6 @@ func calculateNewton(start float64) float64 { |
|
|
|
|
} |
|
|
|
|
return x |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
assert.Equal(t, calculateNewton(1), module.IO[1]) |
|
|
|
|
} |
|
|
|
|