Testing objects for equivalence

October 29 2006 Karel Srot 216
Maple
0

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, `
a:=lhs(eq1)-rhs(eq1);

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

> elif hastype(eq1, `=`) or hastype(eq1, `

> 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

> 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]];

> mytesteq(%,%);

>

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

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

> mytesteq(%,%%);

>

> exp(I*x)>=0;

> 0<>

> mytesteq(%,%%);

>

> exp(I*x)>=0;

> 1;

> mytesteq(%,%%);

>

> exp(I*x)>=0;

> exp(I*x)>0;

> mytesteq(%,%%);

> exp(I*x)>=0;

> exp(I*x)>=1;

> mytesteq(%,%%);

>

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

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

> mytesteq(%,%%);

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

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

> mytesteq(%,%%);

>

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

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

> mytesteq(%,%%);

>

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

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

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

> mytesteq(A_a,A_b);

>

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

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

> mytesteq(%,%%);

>

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


Please Wait...