Indices#
Index sets are of two types:
collection, e.g. different types of planetary objects in our solar system.
ordered, e.g. hours in a day, days in a year
from gana import Prg, I
p = Prg()
Declaring Sets#
Collections#
For a collection, elements are based as strings, with an optional tag.
p.giants = I('jupiter', 'saturn', 'uranus', 'neptune', tag='giant planets')
p.rocky = I('mercury', 'venus', 'earth', 'mars', tag='rocky planets')
p.gasgiants = I('jupiter', 'saturn', tag='gas giants')
p.minor = I('ceres', 'pluto', 'makemake', 'eris', tag='minor planets')
p.asteroid = I('ceres')
p.former = I('pluto', tag='used to be a planet :(')
p.voyager = I(
'saturn',
'neptune',
'uranus',
'titan',
'earth',
'jupiter',
tag='visited by the Voyager program',
)
Ordered Sets#
Only the size is needed
p.days = I(size=365, tag="days in a year", mutable=True)
p.days.show()
Set Operations#
These only apply for Collections
Set Minus#
p.icegiants = p.giants - p.gasgiants
p.icegiants.show(True)
And#
p.hasheart = p.minor & p.former
# p.hasheart = p.minor and p.former
p.hasheart.show(True)
Or#
p.planets = p.gasgiants | p.icegiants | p.rocky
p.planets.show(True)
Exclusive Or#
p.dense = p.voyager ^ p.giants
p.dense.show(True)
Cartesian Product#
p.i = I('a', 'b', 'c')
p.j = I('p')
p.k = I(size=2)
p.l = p.i * p.j * p.k
p.l
(i, j, k)
\(\LaTeX\)#
Use:
.latex() to return string
.show() to pretty print in console
With Elements#
Elements are shown for both types of sets, but upto a limit.
p.minor.show()
Beyond a default length of 5 elements dots (\(\dots\)) are used.
p.voyager.show()
This can however be extended
p.voyager.show(dots_limit=7)
Integer Notation#
This can only be used for ordered sets
p.days.show(True, int_not=True)
Only Name#
For only the name, turn descriptive print off.
p.giants.show(False)
Accessing Elements#
Irrespective of type, elements can be accessed by position. Thus, even collections are not truly sets in the strictest sense
p.voyager[3], p.days[33]
(titan, days[33])
Additionally, elements are set on the Program as index sets of size 1
p.titan, len(p.titan)
(titan, 1)
To return just the \(\LaTeX\) string, use .latex()
p.days.latex()
'{days} = \\{ {{{{days}_{0}}}, \\dots ,{{{days}_{364}}}} \\}'