Nested Verification

alec's picture

Nested verification can be done in Maple using the following command,

VerifyTools:-AddVerification('nested'=proc(x,y,ver::verification)
if whattype(x)=whattype(y) then 
if x::Vector then verify(x,y,'Vector'('nested'(args[3..-1])))
elif x::Matrix then verify(x,y,'Matrix'('nested'(args[3..-1])))
elif x::Array then verify(x,y,'Array'('nested'(args[3..-1])))
elif x::array then verify(x,y,'array'('nested'(args[3..-1])))
elif x::set then verify(x,y,'set'('nested'(args[3..-1])))
elif x::list then verify(x,y,'list'('nested'(args[3..-1])))
elif x::relation then verify(x,y,'relation'('nested'(args[3..-1]))) 
elif x::range then verify(x,y,'range'('nested'(args[3..-1]))) 
else verify(x,y,args[3..-1]) fi 
else verify(x,y,args[3..-1])
fi end);

It works as follows,

verify(expr1,expr2,nested);

or

verify(expr1,expr2,nested(some_verification));

where some_verification should be substituted for some verification name. For example,

A:=<<[[exp(I*x),x-x^2],{sin(x)^2,3}],(x-x^2<0)>|
<(5=y-z),{{[[1..2,3..x^2+x],5],3},(y-1)^2}>>;

  A :=

        [                 2             2              ]
        [[[exp(x I), x - x ], {3, sin(x) }] , 5 = y - z]

        [     2                            2                   2 ]
        [x - x  < 0 , {{3, [[1 .. 2, 3 .. x  + x], 5]}, (y - 1) }]

B:=<<[[cos(x)+I*sin(x),x*(1-x)],{2+sin(x)^2+cos(x)^2,1-cos(x)^2}],(x<x^2)>|
<(z=y-5),{y^2-2*y+1,{3,[[cosh(x)^2-sinh(x)^2..2.,3.0..x*(x+1)],5.0000]}}>>;

  B :=

        [
        [[[cos(x) + sin(x) I, x (1 - x)],

                   2            2         2              ]
        {1 - cos(x) , 2 + sin(x)  + cos(x) }] , z = y - 5]

        [     2     2
        [x < x  , {y  - 2 y + 1,

                     2          2
        {3, [[cosh(x)  - sinh(x)  .. 2., 3.0 .. x (x + 1)], 5.0000]}}

        ]
        ]

verify(A,B,nested(testeq));

                                 true

Another example,

verify([[2,3],{1,5}],[[2.,3.0],{1.,5.00}],nested);

                                 true

______________
Alec Mihailovs
http://mihailovs.com/Alec/

Comments

functional form of nested verification

Here's a less efficient but more compact version, using a functional style of programming. A trivial advantage is that it is easier to add new verifications (I added record), since they only appear once.

VerifyTools:-AddVerification(
    'nested' = proc(x, y, ver:=NULL)
        whattype(x)=whattype(y)
        and ormap(t -> (x::t and verify(x,y,'t'('nested'(ver))))
                  , [Vector,Matrix,Array,array,set,list,relation,range,record])
        or verify(x, y, ver);
    end proc ):
alec's picture

Compact and efficient form of nested verification

It can be done compact and efficient by using a mix of procedural and functional programming,

VerifyTools:-AddVerification('nested'=proc(x,y,ver:=NULL) local t,f;
f:=t->verify(x,y,'t'('nested'(ver)));
for t in [Vector,Matrix,Array,array,set,list,relation,range,record]
do if x::t then break fi od;
if y::t then f(t) else verify(x,y,ver) fi
end);

I usually write a working procedure first, and optimize it after.

__________
Alec Mihailovs
http://mihailovs.com/Alec/

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}