Testing objects for equivalence

UPDATE!!! I found that my blogpost appers in Maple reporter. You should know, that this blogpost was just a question about how to solve my problem better. Alec Mihailovs gave me and aswer and his solution of testing nested objects is much better. If you still want to use my piece of code, you should remove "set object" form "convertAMVStolist" procedure and replace all occurrences of "hastype" with "type" (as mentioned in comments below).


This piece of code should be able to test two objects (not of every type) for equivalence (like testeq() does). The benefit is, that it should be able to test also nested objects. Is there any other and more simple way how to do that? How to test nested objects in sets?

This procedure converts objects such as Array, Matrix, Vector and set into listlist eventually. list. Other objects remains.

convertAMVStolist:=proc(eq) local a;

if hastype(eq, 'Matrix') or hastype(eq, 'Array') or hastype(eq, 'Vector') or hastype(eq, 'set') then

a:=convert(eq, 'listlist');

else

RETURN(eq);

end if;

RETURN(a);

end:

This procedure tests two parameters for equivalency (similar like testeq() does). Parameters may be algebraic expressions, lists, sets, matrices, vectors, ...

Objects can be nested, just sets can contain only algebraic expressions.

Sorry that the code is not well commented, my english is not good.

mytesteq:=proc(eq1, eq2) local a, b, i, result;

if the objects are equations, inequations.. subtract right side from left.

if hastype(eq1, whattype(eq2)) and hastype(eq2, whattype(eq1)) and (hastype(eq1, `=`) or hastype(eq1, `<=`) or hastype(eq1, `<`)) then
a:=lhs(eq1)-rhs(eq1);

b:=lhs(eq2)-rhs(eq2);

elif hastype(eq1, `=`) or hastype(eq1, `<=`) or hastype(eq1, `<`) or hastype(eq2, `=`) or hastype(eq2, `<=`) or hastype(eq2, `<`) then

print(`equations/unequations - different type`);

RETURN(false);

else
a:=eq1;

b:=eq2;
end if;

now I will process other types

matrices, vectors, arrays will be converted into lists

if (hastype(a, 'Matrix') and hastype(b, 'Matrix')) or (hastype(a, 'Array') and hastype(b, 'Array')) or (hastype(a, 'Vector') and hastype(b, 'Vector')) then

print(`matrices, arrays, vectors - converting into list`);

a:=convertAMVStolist(a);

b:=convertAMVStolist(b);

testing if parameters have different types

elif hastype(a, 'Matrix') or hastype(b, 'Matrix') or hastype(a, 'Array') or hastype(b, 'Array') or hastype(a, 'Vector') or hastype(b, 'Vector') then

print(`matrix, array, vector and other type`);

RETURN(false);

end if;

lists

if hastype(a, 'list') and hastype(b, 'list') then

if nops(a) <> nops(b) then

print(`lists have different length`);

RETURN(false);

end if;

now I walk through the lists (nested lists) and compare each members.

print(`list, testing nested objects`);

result:=true;

i:=1;

while (i<=nops(a)) and result do

result:=mytesteq(op(i,a), op(i,b));

i:=i+1;

end do;

RETURN(result);

.testing if parameters have different types

elif hastype(a, 'list') or hastype(b, 'list') then

print(`list and other type`);

RETURN(false);

end if;

sets

if hastype(a, 'set') and hastype(b, 'set') then

print(`sets, testing members using testeq()`);

RETURN(verify(a, b, 'set(testeq)'));

elif hastype(a, 'set') or hastype(b, 'set') then

print(`set and other type`);

RETURN(false);

end if;

if I proceed here, use testeq() as default

print(`default test using testeq`);

print(value(eval(a)));

print(value(eval(b)));

RETURN(testeq(value(eval(a)),value(eval(b))));

end:

And now some examples.

[1,2,[3,4]];

Maple Equation

mytesteq(%,%);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

[1,cos(x)+I*sin(x),[cos(x)^2]];

Maple Equation

[1,exp(I*x),[1-sin(x)^2]];

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

exp(I*x)>=0;

Maple Equation

0<=cos(x)+I*sin(x);

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

exp(I*x)>=0;

Maple Equation

1;

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

exp(I*x)>=0;

Maple Equation

exp(I*x)>0;

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

exp(I*x)>=0;

Maple Equation

exp(I*x)>=1;

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Matrix([[x*(1-x),2,3],[4,1-cos(x)^2,6]]);

Maple Equation

Matrix([[x-x^2,2,3],[4,sin(x)^2,6]]);

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Matrix([[x*(1-x),2,3],[4,1-cos(x)^2,6]]);

Maple Equation

Matrix([[x*(1-x),2,3],[4,1-cos(x)^2,5]]);

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

{1,x*(x-1)};

Maple Equation

{cos(x)^2+sin(x)^2,x^2-x};

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

A_a1:=[[x*(1-x),2,exp(I*x)],[4,1-cos(x)^2,6]]:

A_a:=Array(1..2,1..3,A_a1);

Maple Equation

A_b1:=[[x-x^2,2,cos(x)+I*sin(x)],[4,sin(x)^2, 6]]:

A_b:=Array(1..2,1..3,A_b1);

Maple Equation

mytesteq(A_a,A_b);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Vector[row]([exp(I*x),x*(x-1),3]);

Maple Equation

Vector[row]([cos(x)+I*sin(x),x^2-x,3]);

Maple Equation

mytesteq(%,%%);

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Maple Equation

Now my problem is: I would like to test sets (and its members and its members etc.) in same way as other objects. Is there simple way how to do it? Or is there more simple way how to do things above?

Thank you for you suggestions.

This post was generated using the MaplePrimes File Manager

View worksheet on MapleNet or Download it

Comments

alec's picture

Adding new verification procedures

I already posted that in the forums, but I repost it here just so that it could be found more easily.

A new verification procedure can be added as in the following example,

VerifyTools:-AddVerification(example=proc(x,y) evalb(x=y^2) or evalb(y=x^2) end);

verify(1,3,example);
                                false
verify(2,4,example);
                                 true

A series of examples of verification procedures can be found through

kernelopts(opaquemodules=false):
interface(verboseproc=2):
print(VerifyTools:-VerifyTab);

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

alec's picture

hastype vs. type

  1. It seems as if one should use type instead of hastype in your procedures.
  2. convert/listlist doesn't work with sets, so convertAMVStolist(eq) would give an error if eq is a set. It could be written as
    clist:=eq->if eq::{array,rtable} then convert(eq,listlist) 
    elif eq::set then convert(eq,list) else eq fi:

    but it may be not a good idea to convert sets to lists, because converted lists may be not equal (they may be ordered differently) for equal sets.

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

You are right. Conversion of

You are right. Conversion of set in convertAMVStolist(eq) doesn't work. Actually the conversion should not be there, it's a relic from older version of code.

I thought that the right way is in using verify(), but I did not know how tu extend it. Thank you very much for your help.

alec's picture

A procedure for nested verification

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(some_verification));

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/

Comment viewing options

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