“Code” With Math


QUICK NOTE:

This post was made with the sole intention of sharing these functions and is by no means a “good read”, so reduce your expectations to zero as this is basically just a helpful list.

OTHER NOTE:

The binary 1 and 0 outputting functions are actually called “indicator functions”, and mathematicians do not actually write these out, and instead use a shorthand notation such as: I(condition), where the function is just a piecewise function. I made up the term “logic-bearing functions” to describe these written out functions plus the ones that output other numbers. “Logic-bearing functions” is not a technical math term or anything (but it could be!)


Usually when mathematicians need a function to follow specific instructions depending on the input values, a piecewise function is used. However, there are certain situations when a piecewise function is not available or inconvenient.

For example, if someone wants to make an equation for a series of numbers that alternates between negative and positive values of ‘1/n’, they could use a piecewise equation that says “if n%2=1; -1/n, if n%2=0; 1/n”, but this would be more difficult for the reader to use, and you wouldn’t be able to evaluate it as a summation in most calculators.

A solution to this would be to make the function automatically become negative when an odd input is used. We can do this by writing the function as “(1/n)(-1)^n”. With this version, you can easily type the equation into any calculator or online summation program and the function will be evaluated exactly how it is supposed to.

This way of writing math so that all of the logical heavy lifting is done for you resembles computer programming and can make evaluating certain problems, particularly integrals and summations, much easier.

Below I have compiled a list of functions that conditional logic on functions that they are applied to, I call them logic-bearing functions. Some of these I have found, and some of these I have come up with myself.

I wrote all of these in single text line format so that copying and pasting them would be easier, but if you would like to see a more visible version, paste the equation into a site that formats math equations, such as Wolfram Alpha. I may update this post in the future so that the visible versions are displayed as well.

IMPORTANT NOTE: Some of these functions use the assumption: 0^0=0. This is a contentious topic in mathematics, and as a result, some programming languages and calculators may not support the use of 0^0, and will cause an output to be “undefined”. However, most of the functions that incorporate ‘0^|x|’ will still work in most cases. A function that incorporates 0^|x| on a grapher that does not support 0^0 will still (usually) produce a visually identical result, so even though the sign and inequality functions produce an error at zero and equivalence, this won’t show up on the graph.

In the event that you need to check for equivalent values and cannot use 0^0, I have included an alternative in the (in)equality section, however, it does require the ceiling operation.

>>>>POSITIVE/NEGATIVE<<<<

This section is all about the sign of the values that are input in a function. These logic-bearing functions output simple results depending on if the function is positive, negative or zero.

IF ODD; [-A], IF EVEN; [A]: [A]*[-1]^X

IF EVEN; [-A], IF ODD; [A]: [A]*[-1]^[X+1]

These two functions switch the function “A” from producing a positive output to producing a negative output, depending on if the input value, “X”, is odd or even. This logic-bearing function is used extensively in alternating series.

SIGN FUNCTION(X): [X/(|X|+0^|X|)]

The sign function, denoted “sgn” so that people don’t confuse “sign” with “sine”, is a function that returns a value of 1, -1, or 0, depending on if the input value is positive, negative, or zero.

IF NEGATIVE,-1; ELSE, 1: [(X+0^|X|)/(|X|+0^|X|)]

IF NEGATIVE->0, ELSE-> 1: [(X+0^|X|)/(|X|+0^|X|)+1]/2

The above two functions are similar to the sign function, but here they return only two possible results, depending the input value’s sign. There are many situations where negative numbers require drastically different results, and these functions can be used to fix the problems that arise from that.

>>>>BINARY_INPUTS<<<<

The following functions are designed to emulate what binary logic operators such as AND, OR, NOT, and XOR do. As a consequence of being designed for binary logic, these functions only work when their inputs are in positive-single-digit binary and themselves only output positive-single-digit binary. While theses functions are very simple and there are only four of them listed here, they can be combined with other positive-single-digit binary to run more complicated logic. We will use these later in the (in)equality and memory sections.

AND(A,B): (A*B)

OR(A,B): (A+B)/2^(A*B)

NOT(A): [1-A]

XOR(A,B): [1-(A*B)]*[(A+B)/2^(A*B)]

Any of these three logic functions can be combined with “NOT” to produce “compound logic operators” such as “XNOR”, “NOR”, and “NAND”.

>>>>MIN/MAX<<<<

MAX(A,B): (0.5)(A+B+|A-B|)

MIN(A,B): (0.5)(A+B-|A-B|)

The former two functions output the minimum or maximum value of “A” and “B”. These can be especially helpful in setting bounds with definite integrals.

>>>>(IN)EQUALITY<<<<

The following logic-bearing functions produce positive-single-digit binary results where 1 means the (in)equality is TRUE, and 0 means the (in)equality is FALSE.

A=B: 0^|A-B|

A=B <ALTERNATIVE>: [1-ceil[|A-B|/(|A-B|+1)]]

The above two functions check to see if the input values are exactly identical, and can be useful when graphically checking when an equation is true. If 0^0 evaluates to be

A<=X: [[(X-A)/(|X-A|+0^|X-A|)]+1+0^|X-A|]/2

A<X: [[[(X-A)-0^|X-A|]/(|X-A|+0^|X-A|)]+1]/2

X<=B:[[(B-X)/(|B-X|+0^|B-X|)]+1+0^|B-X|]/2

X<B:[[[(B-X)-0^|B-X|]/(|B-X|+0^|B-X|)]+1]/2

A<X<B: [[[[(X-A)-0^|X-A|]/(|X-A|+0^|X-A|)]+1]/2]*[[[[(B-X)-0^|B-X|]/(|B-X|+0^|B-X|)]+1]/2]

The former five inequalities can be especially useful for piecewise functions that require certain functions to be used for specific ranges. Do note: the final combined inequality is actually just two inequalities combined by a logical “AND” operator.

>>>>MEMORY/ACTIVATION<<<<

S={A.B.C}; S[M]: [A*(0^|M|)+B*(0^|1-M|)+C*(0^|2-M|)]

This function works just like selecting an index from an array in a programming language. Here, if the index “M” you have selected matches a value in the equality operator, the function will return only the value it is next to , while the other values in the array will be multiplied by zero and therefore cancelled out.

IF M==Z, RUN A; ELSE, RUN B: 0^|M-Z|*[A]+[1-0^|M-Z|]*[B]

Here we use a similar system as above, but we use a “NOT” operator that makes a different function, “B” run when the values “M” and “Z” are not equivalent.

FOR X<Z, RUN A; ELSE, RUN B: [[[[(Z-X)-0^|Z-X|]/(|Z-X|+0^|Z-X|)]+1]/2]*[A]+[1-[[[[(Z-X)-0^|Z-X|]/(|Z-X|+0^|Z-X|)]+1]/2]]*[B]

Here we write the exact same function as the one above, except the function “A” runs for a range of values, and not just one.

Leave a comment