Skip to content

Custom SageMath Functionality

Drew Lewis edited this page Jun 17, 2025 · 13 revisions

Authors can use the custom TBIL SageMath module in SagePlots:

<image>
  <sageplot>
<xi:include parse="text" href="../../../common/sagemath/library.sage"/>
# code goes here, fully flush left (the above line needs to also be fully flushed left)
  </sageplot>
</image>

and in CheckIt generators:

# at top of file
load("../../../source/common/sagemath/library.sage")

See below for various options, along with their defaults if not provided. (If this page gets stale, see the code at https://github.com/TeamBasedInquiryLearning/library/blob/main/source/common/sagemath/library.sage for the most up-to-date versions.)

Functions

special_angles

TBIL.special_angles(
    n=0,
    min=0,
    max=2*pi,
    include_min=True,
    include_max=False,
    list=False)

Generates a list of n random special angles (i.e. multiples of $\dfrac{\pi}{4}$ and $\dfrac{\pi}{6}$) from the interval [min,max). If list is True, it instead returns the full list of special angles in the provided range.

plot_angle

TBIL.plot_angle(
    angle, # in radians
    reference_angle=0,
    show_axes=True,
    show_unit_circle=False,
    show_unit_point=False,
    label_unit_point=False,
    show_angle_value=False,
    show_reference_angle_value=False,
    show_triangle=False,
    triangle_labels=False,
    degree_labels=False,
)

plot_angle() is a function useful for plotting diagrams showing angles around the unit circle. It takes one mandatory argument, the angle, and other arguments can be set to True to provide various decorations.

TBIL.plot_angle(pi/3,show_unit_circle=True)

image

Argument Default Value Description
angle N/A Angle to plot, in radians
reference_angle 0 Offset angle
show_axes True Controls whether $x$ and $y$ axes are shown
show_unit_circle False Controls whether unit circle is shown
show_unit_point False Controls whether a point is drawn on the unit circle at the end of the ray
label_unit_point False Controls whether the unit point is labeled. If set to True, will compute the $x$- and $y$-coordinates and display them. If set to a tuple of length 2, will display the tuple elements as the $x$- and $y$-coordinates.
show_angle_value False Controls whether the given angle is labeled. If a string is passed, that string will be used as the label. If any other true value is passed, the angle measure will be used, displayed in radians (unless degree_labels is set to True)
show_reference_angle_value False Controls whether the supplied offset angle is labeled with its measure
show_triangle False Controls whether a right triangle is drawn
triangle_labels False Controls the appearance of labels of the lengths of the legs of the right triangle. If set to True, will compute the lengths and display them. If set to a tuple of length 2, will display the tuple elements as the labels of the horizontal and vertical legs, respectively.
degree_labels False If set to True will display the angle measures in degrees. Note the supplied angle should still be in radians.

trig_plot

TBIL.trig_plot(f,(xmin,xmax),ticks=pi/4)

Generates a plot with x-axis labels at rational multiples of $\pi$, with nice typesetting. Default is to have ticks set at integer multiples of $\frac{\pi}{4}$, but this can be controlled by the ticks argument.

TBIL.trig_plot(sin(x),(-2*pi,2*pi),ticks=pi/2)

image

Further customizations can be passed, which are passed along to the underlying sage plot method.

TBIL.trig_plot(sin(x),(0,2*pi),color="green",thickness=3,linestyle="dashed")

image

typeset_angle

TBIL.typeset_angle(
    theta,
    latex_delimiters=False
)

Returns a LaTeX formatted string that typesets rational expressions $\dfrac{3\pi}{2}$, rather than $\frac{3}{2}\pi$ as Sage produces. Despite the name, this is also handy for expressions like $\dfrac{\sqrt{3}}{2}$ that Sage likes to typeset as $\frac{1}{2}\sqrt{3}$. Expressions that are not fractions are not altered. For convenience, set latex_delimiters=True to have the string begin and end with $.

numberline_plot

TBIL.numberline_plot(
    center=0,
    radius=10
)

Generates a numberline. You can specify the center as well as the radius.

TBIL.numberline_plot(center=5,radius=15)

image

inequality_plot

TBIL.inequality_plot(
    start=None, 
    strict_start=True, 
    end=None, 
    strict_end=True,
    label_endpoints=True,
    scale=10
)

Generates decorations for a numberline_plot to represent inequalities. To have endpoints at infinity, you can omit the start or end parameters. The scale parameter should match the radius of a numberline_plot; both default to 10. You can add multiple of these to a plot if you need several intervals.

p=TBIL.numberline_plot(radius=15)
p+=TBIL.inequality_plot(start=-10,strict_start=True,end=-6,strict_end=False,label_endpoints=False,scale=15)    
p+=TBIL.inequality_plot(start=8,strict_start=False,label_endpoints=False,scale=15)
p.axes(False)

image

intervals_from_inequality

TBIL.intervals_from_inequality(
  inequality,
  partition_points,
  undefined_points=None,
  checkpoints=None
)

This is a helper function, primarily used in Checkit, to generate a list of intervals where an inequality is true. The returned list of strings is suitably formatted to use with TBIL.numberline_from_intervals(). You should supply a list of partition_points, between which it is assumed that the truth value of the inequality is constant. If the inequality is undefined at any point, those should be supplied as well. The function determines if an inequality is true on an interval by evaluating at the points in checkpoints; if these are not supplied, these are either the midpoint (of a finite interval) or for infinite intervals one more or less than the finite endpoint.

numberline_from_intervals

TBIL.numberline_from_intervals(
  intervals
)

This can be combined with intervals_from_inequality() to generate a numberline plot of where the inequality is true.

TBIL.numberline_from_intervals(TBIL.intervals_from_inequality(x^2-4*x-5>0,partition_points=[-1,5]))

image

You can also manually pass it a list of some Latex formatted strings. Remember to either escape backslashes or use a raw string (prefix with 'r'). This doesn't currently have good error handling for improperly formatted strings.

p=TBIL.numberline_from_intervals([r"(-\infty,-4)","[3,5)"])

image

small_rationals

def small_rationals(
    numerators=range(-8,9), 
    denominators=None, # defaults to [2,3,5]
    dictionary=True,
    length=1
)
    '''Generates a list or dictionary of unique rational numbers with small numerators and denominators. 
    For a dictionary, keys are the rationals and values are the denominators.'''

small_irrationals

TBIL.small_irrationals(
    rational_part=range(-8,9), 
    irrational_part=None,
    denominators=None,
    dictionary=True,
    length=1,
    full_list=False
)
'''Generates a list or dictionary of uniqe irrational numbers of the form (a+sqrt(b))/c. 
For a dictionary, keys are tuples of rationals and their conjugate, and values are the denominators.'''

small_complex

TBIL.small_complex(
    real_part=range(-5,6), 
    imaginary_part=[i for i in range(-5,6) if i != 0],length=1
)
'''Generates a list of unique 2-tuples of conjugate pairs of complex (not real) numbers of the form a+bi.'''

line_from_points

TBIL.line_from_points(point1,point2)
'''Returns the equation of a line through the two given points'''

line_from_point_slope

TBIL.line_from_points(point,slope)
'''Returns the equation of a line from a point and a slope'''

config_matrix_typesetting

TBIL.config_matrix_typesetting()

This globally sets matrices to display with bracket delimiters and centered columns.

choices_from_list

TBIL.choices_from_list(lst)
"""Given a list, return a list of choices in a canonical way, in a random order.
The first item of the list is the "correct" choice."""

Classes

RowOp

SetBuilder

BracedSet

VectorSet

VectorList

LinearCombination

Equation

VectorEquation

Clone this wiki locally