Skip to content

Commit

Permalink
new (transient) implementation for transform. Still, newer one to fol…
Browse files Browse the repository at this point in the history
…low immediately after this commit.
  • Loading branch information
davidedc committed Sep 8, 2016
1 parent f82e9df commit c3d22d9
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 65 deletions.
66 changes: 58 additions & 8 deletions run-micro-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ run_test [
run_test [

"pattern(dot(transpose(a_),a_), cov(a_))",
"",
"dot(transpose(a_),a_)->cov(a_)",

"pattern(dot(a_,transpose(a_)), cov(a_))",
"",
"dot(a_,transpose(a_))->cov(a_)",

#"pattern(cov(transpose(a_)), cov(a_))",
#"",
Expand Down Expand Up @@ -78,7 +78,7 @@ run_test [
run_test [

"pattern(dot(transpose(a_),a_), cov(a_))",
"",
"dot(transpose(a_),a_)->cov(a_)",

"simplify(integral(1/(X-A)/sqrt(X^2-A^2),X)+sqrt(X^2-A^2)/A/(X-A))",
"0",
Expand All @@ -101,7 +101,7 @@ run_test [
run_test [

"pattern(dot(a_,transpose(a_)), cov(a_))",
"",
"dot(a_,transpose(a_))->cov(a_)",

"simplify(eig(dot(transpose(A+B),B+transpose(transpose(A)))))",
"eig(cov(A+B))",
Expand Down Expand Up @@ -129,10 +129,10 @@ run_test [
run_test [

"pattern(dot(transpose(a_),a_), cov(a_), not(number(a_)))",
"",
"dot(transpose(a_),a_)->cov(a_)",

"pattern(dot(transpose(a_),a_), cov(a_), number(a_),a_>0 )",
"",
"dot(transpose(a_),a_)->cov(a_)",

"simplify(eig(dot(transpose(3),transpose(transpose(3)))))",
"eig(9)",
Expand All @@ -151,7 +151,7 @@ run_test [
run_test [

"pattern(something(a_,b_),b_*somethingElse(a_))",
"",
"something(a_,b_)->b_*somethingElse(a_)",

"simplify(something(x,y))",
"somethingElse(x)*y",
Expand All @@ -164,7 +164,7 @@ run_test [
run_test [

"pattern(something(a_,b_),b_*somethingElse(a_))",
"",
"something(a_,b_)->b_*somethingElse(a_)",

"indirection(h,k) = something(h,k)",
"",
Expand All @@ -178,5 +178,55 @@ run_test [
]


run_test [

"pattern(dot(a_,transpose(a_)), cov(a_))",
"dot(a_,transpose(a_))->cov(a_)",

"simplify(1 + eig(dot(transpose(A)+transpose(B),B+transpose(transpose(A)))))",
"1+eig(inner(transpose(A),A)+inner(transpose(A),B)+inner(transpose(B),A)+inner(transpose(B),B))",

# catches if a guard works against substituting bare native functions
"simplify(1 + eig(dot(transpose(A)+transpose(B),B+transpose(transpose(A)))))",
"1+eig(inner(transpose(A),A)+inner(transpose(A),B)+inner(transpose(B),A)+inner(transpose(B),B))",

"clearsubstrules()",
"",

]


run_test [

"pattern(dot(transpose(a_),a_), cov(a_))",
"dot(transpose(a_),a_)->cov(a_)",

# picks up that transpose(abs(k))
# is a substitution that works
"simplify(1 + eig(dot(abs(k),transpose(abs(k)))))",
"1+eig(cov(transpose(abs(k))))",

# picks up that transpose(b(2))
# is a substitution that works
"simplify(1 + eig(dot(b(2),transpose(b(2)))))",
"1+eig(cov(transpose(b(2))))",

"clearsubstrules()",
"",

]

run_test [

"pattern(a_ + b_, dot(cov(b_),cov(a_)))",
"a_+b_->dot(cov(b_),cov(a_))",

"simplify(something + somethingelse)",
"inner(cov(somethingelse),cov(something))",

"clearsubstrules()",
"",

]

# alert "passed tests: " + ok_tests + " / failed tests: " + ko_tests
2 changes: 1 addition & 1 deletion run-tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ selftest = ->
test_eigen()
test_shape()
mini_test()
test_roots()
test_integral()
test_roots()


# alert "passed tests: " + ok_tests + " / failed tests: " + ko_tests
Expand Down
42 changes: 28 additions & 14 deletions sources/decomp.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ Eval_decomp = ->
list(tos - h)
restore()


pushTryNotToDuplicate = (toBePushed) ->
if tos > 0
if DEBUG then console.log "comparing " + toBePushed + " to: " + stack[tos-1]
if equal(toBePushed, stack[tos-1])
if DEBUG then console.log "skipping " + toBePushed + " because it's already on stack "
return
push(toBePushed)


# returns constant expressions on the stack

decomp = (generalTransform) ->
Expand All @@ -30,20 +40,20 @@ decomp = (generalTransform) ->
p2 = pop()
p1 = pop()

if DEBUG then console.log "DECOMPOSING " + p1
if true then console.log "DECOMPOSING " + p1

# is the entire expression constant?

if generalTransform
if !iscons(p1)
if DEBUG then console.log "ground thing: " + p1
push p1
if true then console.log " ground thing: " + p1
pushTryNotToDuplicate p1
restore()
return
else
if (Find(p1, p2) == 0)
if DEBUG then console.log "entire expression is constant"
push(p1)
if true then console.log " entire expression is constant"
pushTryNotToDuplicate(p1)
#push(p1); # may need later for pushing both +a, -a
#negate()
restore()
Expand All @@ -58,26 +68,27 @@ decomp = (generalTransform) ->

# product?

if (car(p1) == symbol(MULTIPLY))
if (ismultiply(p1))
decomp_product(generalTransform)
restore()
return

# naive decomp if not sum or product

if DEBUG then console.log "naive decomp"
if true then console.log " naive decomp"
p3 = cdr(p1)
if DEBUG then console.log "startig p3: " + p3
while (iscons(p3))
if DEBUG then console.log "recursive decomposition"
push(car(p3))

# for a general transformations,
# we want to match any part of the tree so
# we need to push the subtree as well
# as recurse to its parts
if generalTransform
push(car(p3))

if DEBUG then console.log "recursive decomposition"
push(car(p3))

if DEBUG then console.log "car(p3): " + car(p3)
push(p2)
Expand All @@ -88,14 +99,16 @@ decomp = (generalTransform) ->
restore()

decomp_sum = (generalTransform) ->
if true then console.log " decomposing the sum "
h = 0


# decomp terms involving x

p3 = cdr(p1)

while (iscons(p3))
if (Find(car(p3), p2))
if (Find(car(p3), p2) or generalTransform)
push(car(p3))
push(p2)
decomp(generalTransform)
Expand All @@ -109,25 +122,26 @@ decomp_sum = (generalTransform) ->

while (iscons(p3))
if (Find(car(p3), p2) == 0)
push(car(p3))
pushTryNotToDuplicate(car(p3))
p3 = cdr(p3)

if (tos - h)
add_all(tos - h)
p3 = pop()
push(p3)
pushTryNotToDuplicate(p3)
push(p3)
negate(); # need both +a, -a for some integrals

decomp_product = (generalTransform) ->
if true then console.log " decomposing the product "
h = 0

# decomp factors involving x

p3 = cdr(p1)

while (iscons(p3))
if (Find(car(p3), p2))
if (Find(car(p3), p2) or generalTransform)
push(car(p3))
push(p2)
decomp(generalTransform)
Expand All @@ -141,7 +155,7 @@ decomp_product = (generalTransform) ->

while (iscons(p3))
if (Find(car(p3), p2) == 0)
push(car(p3))
pushTryNotToDuplicate(car(p3))
p3 = cdr(p3)

if (tos - h)
Expand Down
Loading

0 comments on commit c3d22d9

Please sign in to comment.