Removing \left
's and \right
's from piecewise expressions
(A clarification of this tip that's been edited to oblivion)
When using a piecewise expression, you may have noticed that if you took out the \left
and \right
from a piecewise expression, it doesn't work. So, for example, the following doesn't work when directly pasted into Desmos:
\{x<0:x,x^3\}
But if you include the \left
and \right
, it does work:
\left\{x<0:x,x^3\right\}
To fix this issue, you can add a newline in front of the expression that includes the piecewise. In our case, the code to paste into Desmos is:
\{x<0:x,x^3\}
Even though the actual expression doesn't render properly, the graph of the piecewise expression should still show. Note that the piecewise expression will be fragile, which means that while you have selected the expression, most button presses (including buttons like Ctrl) will break the code.
The downside of doing this trick is that every built-in function with the expression that includes the piecewise is not recognized by the Desmos parser. That means that you have to add a \
in front of every built-in function to force Desmos to recognize it as a built-in function, adding one byte per built-in. For example, the following code won't work:
f(l)=min(\{l=0:l.max,l\})
But the following code does work:
f(l)=\min(\{l=0:l.\max,l\})
As a result, if you use too many built-in functions in the same expression as a piecewise expression, you should consider another approach that doesn't involve any piecewise expressions.
For example, consider the following code (it doesn't do anything useful, but it has a whole bunch of built-in functions):
(64 bytes)
f(l)=\{\sort([1...l.\max+1],\join(l,2,3,l.\mean))=l.\median,0\}
It can be shortened by doing the following:
(60 bytes)
f(l)=0^{(sort([1...l.max+1],join(l,2,3,l.mean))-l.median)^2}
Overall, code in the form:
\{a=b,0\}
which includes \$k\$ built-in functions, can be shortened to:
0^{(a-b)^2}
given that \$k\ge2\$. It will save \$k-1\$ bytes.
Similarly, code in the form:
\{a=b:0,1\}
which includes \$k\$ built-in functions, can be shortened to:
1-0^{(a-b)^2}
given that \$k\ge2\$. It will save \$k-1\$ bytes.
These are just two common examples. Techniques will vary based on what you are trying to do.
Because of this, code that includes piecewise expressions usually will have to be pasted in one expression at a time. This is because if you paste the entire code at once, expressions that don't have a piecewise expression in them will still have to include \
's in front of every built-in function in order to work. If you instead paste in the expressions that include piecewise expressions separately, you can avoid this issue.
For example, consider the following code:
L=l.length
f(l)=\{\sort([1...L],l)<L/2,0\}
If you paste this entire code into Desmos all at once, it will not work, because it expects a \
in front of length
.
But if you first paste in:
L=l.length
Then paste in:
f(l)=\{\sort([1...L],l)<L/2,0\}
The code will work.