Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Utility Module Reference

ben-smith edited this page Feb 9, 2012 · 67 revisions

We just started working on documenting all of the utility functions. A great way to help out and become more familiar with the modules is to help us expand the documentation by editing the reference wiki. Feel free to adjust formatting, re-organize, whatever makes it easier for everyone to find the utility modules they need.

Common Utilities

Less common math stuff

Graphics

Specialized

`cleanMath( expr )`

Simplify formulas before display by removing, eg, + -, - -, and ^1

rand( num )

Returns a random int in [ 0, num ]

digits( n )

Returns an array of the digits of a nonnegative integer in reverse order

digits( 376 ) = [ 6, 7, 3 ]
  • integerToDigits

Returns an array of the digits of a nonnegative integer

digits( 376 ) = [ 3, 7, 6 ]

digitsToInteger

Returns an integer from an array of the digits

digits([ 6, 7, 3 ]) = 673

padDigitsToNum

powerToPlace

roundTowardsZero( a )

Returns the number a with the decimal part truncated.

 roundTowardsZero( 4.6 ) = 4
 roundTowardsZero( -5.9 ) = -5
 roundTowardsZero( 3.3 ) = 3

getGCD( a, b )

Returns the greatest common denominator given two numbers, a and b.

getLCM( a, b )

Returns lowest common multiple given two numbers, a and b.

getPrime

Returns a random prime number.

isPrime( n )

Checks if the given number n is prime.

isOdd( n )

Checks if the given number n is odd.

isEven( n )

Checks if the given number n is even.

getOddComposite

Returns a random odd composite number between min and max. Default values are min = 0 and max = 100. Only returns numbers between 0 and 100, even if min < 0 or max > 100.

getEvenComposite( min, max )

Returns a random even composite number between min and max. Default values are min = 0 and max = 100. Only returns numbers between 0 and 100, even if min < 0 or max > 100.

getComposite()

Returns a composite number between 0 and 100. Unlike getEvenComposite() and getOddComposite(), getComposite() does not take any arguments.

getPrimeFactorization( x )

Returns an array of the prime factors of x.

getPrimeFactorization( 54 ) = [2,3,3,3]

getFactors( number )

Gets the factors of the given number and returns the numbers as an array in order.

getNontrivialFactor( x )

Returns a random factor of x, but not 1 or x itself.

getMultiples( x, upperLimit)

Returns all the multiples of x that are less than or equal to upperLimit.

splitRadical( x )

Simplifies a radical down to it's simplest form. Returns an array of two integers, the first being the number outside the radical sign, and the second being the number inside the radical sign.

 splitRadical( 12 ) = [2,3]

randRange( min, max, count1, count2 )

Gets a random integer between min and max, inclusive.
If a count1 is passed, it gives an 1 dimensional array(1 x count1) of random numbers in the min - max range.
If a count2 is passed, it gives an 2 dimensional array(count1 x count2) of random numbers in the min - max range.

randRangeUnique( min, max, count )

Returns an array (of size count) of unique numbers between min and max

randRangeWeighted( min, max, target, perc )

Gets a random integer between min and max with a perc chance of hitting target (which is assumed to be in the range, but it doesn't have to be).

randRangeExclude( min, max, excludes )

Gets a random integer between min and max that is never any of the values in the excludes array.

randRangeWeightedExclude( min, max, target, perc, excludes )

Get a random integer between min and max with a perc chance of hitting target (which is assumed to be in the range, but it doesn't have to be). It never returns any of the values in the excludes array.

randRangeNonZero( min, max )

Similar to randRange except that zeroes are excluded.

randFromArray( arr, count )

Returns a random member of the given array. If a count is passed, it gives an array of random members of the given array.

randFromArrayExclude( arr, excludes )

Gets a random value from the given array that is not in the excludes array.

roundTo(place, value)

Rounds given value to specified integer place. Negative values round left of the decimal.
Example: roundTo(2, 123.456) returns 123.46

floorTo( precision, x )

Rounds x down to a number of decimal places specified by precision. If precision is negative, then x will be rounded down -precision places to the left of the decimal point.

 floorTo(2, 10.989) = 10.98
 floorTo(2, -10.989) = -10.99
 floorTo(-1, 189.98) = 180

ceilTo( precision, x )

Works the same way as floorTo(), but rounds x up instead of down.

toFraction( decimal, tolerance )

shuffle( array, count )

Shuffles an array using a Fischer-Yates shuffle. If count is passed, returns an random sublist of that size.

sortNumbers( array )

Sorts numbers in an array.

truncate_to_max

restoreSign( x )

Returns 1 if x is positive, or -1 if x is negative.

isInt( x )

Returns true if x is an integer, or false if x is not an integer.

`negParens( n )`

Wraps a number in parenthesis if it is negative.

decimalFraction

reduce( n, d )

Reduces fraction to simplest form. Returns result as an array( n, d ).

toFractionTex

fraction( n, d, defraction, reduce, small, parens )

Formats the latex of the fraction n/d.

  • Will use latex's dfrac unless small is specified as truthy.
  • Will wrap the fraction in parentheses if necessary (ie, unless the fraction reduces to a positive integer) if parens is specified as truthy.
  • Will reduce the fraction n/d if reduce is specified as truthy.
  • Will defraction (spit out 0 if n is 0, spit out n if d is 1, or spit out undefined if d is 0) if defraction is specified as truthy.

mixedFractionFromImproper( n, d, defraction, reduce, small, parens )

Formats the latex of the mixed fraction equivalent of the improper fraction n/d.

For flags defraction, reduce, small, parens, see: mixedFraction()

mixedFraction( number, n, d, defraction, reduce, small, parens )

Formats the latex of the mixed fraction 'num n/d"

  • For negative numbers, if it is a mixed fraction, make sure the whole number portion is negative. '-5, 2/3' should be 'mixedFraction(-5,2,3)'. Do not put negative for both whole number and numerator portion.
  • Will use latex's dfrac unless small is specified as truthy.
  • Will wrap the fraction in parentheses if necessary (ie, unless the fraction reduces to a positive integer) if parens is specified as truthy.
  • Will reduce the fraction n/d if reduce is specified as truthy.
  • Will defract ( i.e. spit out 0 if n is 0, spit out n if d is 1, or spit out undefined if d is 0) if defraction is specified as truthy.

fractionReduce( n, d, small, parens )

Calls fraction with the reduce and defraction flag enabled. Additional parameters correspond to the remaining fraction flags.

fractionSmall

piFraction( d )

Converts a decimal number, d, into the equivalent fraction of pi.

piFraction( 1.57 ) = \frac{1}{2}\pi

reduces

fractionSimplification

mixedOrImproper

splitRadical

formattedSquareRootOf

squareRootCanSimplify

cardinal

Cardinal

quadraticRoots

commafy

plus

randVar

Returns a random algebraic variable (such as "x", or "y" ) to use in expressions.

`plural( ... )`

Pluralization helper

Return “s” if NUMBER is not 1:

plural( NUMBER )

If necessary, magically pluralize and return “NUMBER word”:

plural( NUMBER, singular )

Return “NUMBER word”:

plural( NUMBER, singular, plural )

If necessary, magically pluralize and return “word”:

plural( singular, NUMBER )

Return “word”:

plural( singular, plural, NUMBER )

person( i )

Returns a person’s name from the shuffled list

personVar( i )

Returns the lower case first letter of the person’s name

he( i )

Returns “he” or “she”

He( i )

Returns “He” or “She”

him( i )

Returns “him” or “her”

his( i )

Returns “his” or “her”

His( i )

Returns “His” or “Her”

An( word )

Returns “A”, or “An” if word starts with a vowel

an( word )

Returns “a”, or “an” if word starts with a vowel

vehicle( i )

Returns a type of vehicle from the shuffled list

vehicleVar( i )

Returns the lower case first letter of the vehicle type

course( i )

Returns the name of a course (algebra, Spanish, etc.) from the shuffled list

courseVar( i )

Returns the lower case first letter of the course name

exam( i )

Returns a synonym for exam (test, quiz, etc.) from the shuffled list

binop( i )

Returns an unusual character symbol (diamond, circle, etc.) from the shuffled list.

item ( i )

Returns an item that can be grouped

see also: group() groupVerb()

group( i )

Returns the name of a group of items

see also: item() groupVerb()

groupVerb( i )

Returns the verb describing grouping a type of item

see also: item() group()

store( i )

Returns a type of store/shop

storeItem( i, j )

Returns an item that can be bought at a particular store

  • i: which store in the shuffled list
  • j: which item in the shuffled list

see also: store

pizza( i )

Returns a round food item

exercise( i )

Returns a physical exercise

timeofday( i )

Returns a rough time of day (in the morning, at night, etc.)

school( i )

Returns the name of a school

clothing( i )

Returns an article of clothing

color( i )

Returns the name of a color

fruit( i )

Returns the name of a piece of fruit

deskItem( i )

Returns the name of an office supply

distance( i )

Returns a unit of distance (miles/kilometers)

rode( i )

Returns the past tense verb for an activity

see also: ride() bike() biked() biking()

ride( i )

Returns the infinitive verb for an activity

see also: rode() bike() biked() biking()

bike( i )

Returns the object noun for an activity

see also: rode() ride() biked() biking()

biked( i )

Returns a more specific past tense verb for an activity

see also: rode() ride() bike() biking()

biking( i )

Returns the present progressive tense verb for an activity

see also: rode() ride() bike() biked()

farmer( i )

Returns a type of farmer

see also: crop() field()

crop( i )

Returns the name of the crop grown by a farmer

see also: farmer() field()

field( i )

Returns the type of field used by a farmer

see also: farmer() crop()

side( i )

Returns “left” or “right”

shirtStyle( i )

Returns a style of shirt

math

spin

angles

calculus

trigFuncs

Array of trig basic trig functions. Currently includes sin, cos, and tan.

ddxTrigFuncs

Objects of functions to find the derivative of sin, cos, and tan respectively.

generateFunction( variable )

Generates a random differentiable expression object (function, derivative of the function, and incorrect derivatives)

Variable is what the function of differentiated with respect to. For example a variable of "x", would be the derivative with respect to x.

generateSpecialFunction( variable )

Similar to generateFunction, except with an emphasis on power rule, trig, and e^x / ln x derivatives.

ddxPolynomial( poly )

Finds the derivative of a polynomial. Poly is an instance of Polynomial object.

ddxPolynomialWrong1( poly )

Incorrectly finds the derivative of a polynomial by not decrementing exponents.

ddxPolynomialWrong2( poly )

Incorrectly finds the derivative of a polynomial by incrementing negative exponents.

ddxPolynomialWrong3( poly )

Incorrectly finds the derivative of a polynomial by reversing signs on all terms.

ddxPolynomialWrong4( poly )

Incorrectly finds the derivative of a polynomial by not multiplying coefficients.

congruence

convert-values

exponents

expressions

`Polynomial( minDegree, MaxDegree, coefs, variable, name)`

Creates a new Polynomial object with a degree within the range ["minDegree", "MaxDegree"].

The "coefs" parameter is optional, but will assign the values given as coefficients.

The "variable" parameter will use the given symbol as the variable in the polynomial. (Tip: use randVar() to randomly select a variable.)

The "name" parameter creates an easy reference for the polynomial when generating hints.

graphie

scaleVector

scalePoint

circle

ellipse

arc

path

Creates a path through points in specified in an array.

Each point must be an array of length 2 (ie [x,y]) thus the argument for this function must be a two dimensional array of N x 2 where N must be at >= 2. line

grid

label

plotParametric

plotPolar

plot

init

style

graphie-geometry

graphie-helpers-arithmetic

graphie-helpers

graphie-polygon

interactive

Clone this wiki locally