## small improvements in commands to manipulate sets

Messages in english

compsystems
Messages : 534
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

### small improvements in commands to manipulate sets

Hi Mr. Bernard

In the following link, it talks about introduction to the SETS in maxima CAS
eng: http://maxima.sourceforge.net/docs/manu ... tml#SEC189
spn: http://maxima.sourceforge.net/docs/manu ... tml#SEC190

And in the following link, the examples of the previous page ported to Xcas
https://www.eonicasys.com/CAS/xcas/casc ... as_eng.xws
in some lines with comments, the word "request" is shown, which is what I request. A new Maxima syntax is not intended, but small improvements in commands to manipulate sets

Thank you

Input session (text format)
/* updated: january 3, 2020 */
/* Xcas provides set functions, such as intersection and union, for finite sets that are defined by explicit enumeration. Xcas treats lists and sets as distinct objects. This feature makes it possible to work with sets that have members that are either lists or sets. */
autosimplify( false )
/* // Usage */
set( [] ) // or set[ ], to construct the empty set, Request: simply set()
set( a, b, c ) // or set[a,b,a], to construct a set with members a, b, c
set( a, b, a ) // if a member is listed more than once, internal simplification eliminates the redundant member
set( set[ a ], set( b ) ) // set that have members that are sets.
set( a, set( b ) ) // or set[a,set ], set that have members that can be both sets.
set( a, [ b ] ) // or set[a,], set that have members that are either lists or sets.
/* Two would-be elements x and y are redundant (i.e., considered the same elements for the purpose of set construction) if and only if (x ==y) yields true simbolic value or in other words simplify(x-y) is 0 */
set[ a, b, a ] // the redundant element is a
x := a/c + b/c
y := a/c + b/c
z := (a + b) / c
x == y // request new cmd: is(x = y)
autosimplify( false )
x - y // There is no simplification if the autosimplify flag is off
autosimplify( simplify )
autosimplify( false )
x - y // returns 0 with autosimplify flag is on
normal( x - y ) // maxima: ratsimp( )
simplify( a/c + b/c - (a+b) / c ) // x - z
x == z
x - z
normal( x - z )
y == z
y - z
normal( y - z )
set[ x, y, z ] // x==y therefore x is redundant with y
purge( x, y, z )
set[ x, x ] // x is redundant
set( [ b, a ] ) // to construct a set from the elements of a list, use set(). In maxima: setify();
(x-1) * (x+1) == x² - 1 // is( (x-1)*(x+1) = x^2 - 1 )
simplify( (x-1)*(x+1) = x² - 1 )
simplify( (x-1)*(x+1) == x² - 1 ); x² - 1 == x² - 1
set( [ (x-1)*(x+1) , x² - 1] ) // Further, since ((x - 1)*(x + 1) == x^2 - 1) evaluates to false, (x - 1)*(x + 1) and x^2 - 1 are distinct set members
/* To reduce a set to a set with simplified elements, apply simplification to each member of the set: */
map( normal, set( [ (x-1)*(x+1) , x² - 1 ] ) ) // should return set[x^2-1, x^2-1]
eval( set[x^2-1, x^2-1])
/* To remove redundancies from other sets, you may need to use other simplification functions. Here is an example that uses trigsimp: */
set[ 1, cos(x)² + sin(x)² ]
map( trigsin, set[ 1, cos(x)² + sin(x)² ] ) // should return set[1, 1]
apply( trigsin, set[ 1, cos(x)² + sin(x)² ] ) // should return set[1, 1]
map( lambda x: x², set[ -1, 0, 1 ] )
/* map(set[-1,0,1], lambda x: x^2); // should return set[0, 0, 1] */
/* Some operations on sets, such as substitution, no made a internal simplification to eliminate redundancies; for example */
subst( set[ a, b, c ], c=a )
/* use eval cmd for simplification */
eval( subst( set[ a, b, c ], c=a ) )
size( set[ a, b, a ] )
subst( set[ a, b, c ], [ a=x, b=x, c=x ])
eval( subst( set[ a, b, c ], [ a=x, b=x, c=x ] ))
/* Xcas treats lists and sets as distinct objects; functions such as cartesian product (*) may return unexpected results if any argument is not a set. If you need to apply a set function to a list, use the convert function to convert it to a set. */
[ 1, 2, 3 ] * [ 1, 2, 3 ];
set[ 1, 2, 3 ] * set[ 1, 2, 3 ];
convert( [ 1, 2, 3 ], set ) * convert( [ 1, 2, 3 ], set )
c union set[ a, b ] // Request: accept ( object union set[a,b] )
convert( c, set ) union set[ a, b ] // Request: accept convert( c, set )
set[ c ] union set[ a, b ]
/* Set Member Iteration, There two ways to to iterate over set members. One way is the use map function; for example: */
map( [ a, b, c ], f ) // -> set[f(a),f(b),f(c)]
map( lambda x,y : x==y, set[ 1, 2, 3 ], set[ 1.0, 2.0, 3.0 ] )
apply( x,y -> x==y, set[ 1, 2, 3 ], set[ 1.0, 2.0, 3.0 ] ) // ?
solve( x^3-6*x^2+11*x = 6, x, '=' )
subst( x^3-6*x^2+11*x = 6, set[ x=1, x=2, x=3 ] )
convert( set[ x=1, x=2, x=3 ], list )
map( lambda x : subst( x^3-6*x^2+11*x = 6, x), [ x=1, x=2, x=3 ] )
map( lambda x : evalb( subst( x^3-6*x^2+11*x = 6, x)), [ x=1, x=2, x=3 ] )
/* The other way is to use for iterator */
[ concat( j, 1 ) for j in set[ a, b, c ] ] // ? -> set[ a1, b1, c1 ]
/* Functions and Variables for Sets */
/* append(set a, x) Returns the union of the set a with x. maxima:adjoin() */
append( set[ a, b ], c )
set[ c ] union set[ a, b ]
append( set[ a, b ], c ) == set[ c ] union set[ a, b ] // append (set(a), x) and set(a) union(set(x)) its results are equivalent, even if its members are not in the same order
prepend( set[ a, b ], c )
prepend( set[ a, b ], c ) == set[ c ] union set[ a, b ]
append( set[ a, b ], a )
eval( append( set[ a, b ], a ))
prepend( set[ a, b ], a )
eval( prepend( set[ a, b ], a ))
/* size(set a) returns the number of distinct elements of the set a. size(set a) ignores redundant elements */
size( set[] )
size( set[ a, b, c ] )
size( set[ a, a, b, c ] )
size( set[ A, a, b, c ] )
size( set[ set[] ] )
is_empty( set[ set[] ] ) // Request: new cmd. Return true if and only if is the empty set or the empty list.
/* cartesian_product */
set[ 0, 1 ] * set[ 0, 1 ] //cartesian_product( set[0,1], set[0,1])
set[ x ] * set[ -1, 0, 1 ]
set[ x ] * set[ y ] * set[ z ] //cartesian_product( set[x], set[y], set[z])
/* disjoin (x, set a) Returns the set a without the member x. If x is not a member of a, return a unchanged. */
remove( a, set[ a, b, c, d ] ) // maxima: disjoin(a,set[a,b,c,d]) -> set[b,c,d])
suppres( set[a, b, c, d ], a) // ?
remove( f, set[ a, b, c, d ] )
remove( a + b, set[ 5, z, a + b, pi ] )
select( x -> x >= 5, set[ 1, 2, 6, 7 ] )
/* is_included( x, set a) Returns true if and only if x is a member of the set a. */
is_included( set[ sin(1)], set[ sin(1), sin(2), sin(3) ] ) // request -> true
is_included( sin(1), set[ sin(1), sin(2), sin(3) ] ) // request accept obj as sin(x)
is_included( set[sin(1)], set[cos(1), cos(2), cos(3) ] ) // request -> false