Skip to main content
added 343 characters in body
Source Link
Aiden Chow
  • 14.2k
  • 1
  • 17
  • 55

Edit #2:

(Referring to the example in the first edit)

Apparently you can add a \ at the beginning of the function to make it work(see this comment), which I did not know as well. I guess that invalidates this tip, again.

Edit:

Edit:

Edit #2:

(Referring to the example in the first edit)

Apparently you can add a \ at the beginning of the function to make it work(see this comment), which I did not know as well. I guess that invalidates this tip, again.

Edit:

added 1336 characters in body
Source Link
Aiden Chow
  • 14.2k
  • 1
  • 17
  • 55

Tip invalidated by fireflame241's comment.

Edit:

In fireflame241's comment, he brought to my attention that it is not required to have the \left and \right accompanying the bracket pairs. This is true for most cases. But after some testing, there are some cases where taking out the \left and \right does break the code. Specifically, if you are using any function(e.g. total or max) in addition to these brackets, the function will not work(see example below).

Example:

Suppose you want to compare the corresponding elements of two lists, a and b, and see how many of those corresponding elements are the same. That is, something like a=[1,2,3,4] and b=[2,2,3,5] will output 2(the second and third elements of each list are the same).

Here's what someone might do, after learning that you can take out the \left and \right:

total(\{a=b:1,0\})

In theory, this should work perfectly fine, but in reality, Desmos gives an error and it doesn't work. I'm pretty sure it's because it considers l to be a function, and t, o, and a to be variables in this situation. l, o, and t are not defined, so it gives an error saying so.

In cases like this, it would be better to do:

total(1-sign(a-b)^2)

as suggested by the tip below.


First tip to start it off.

When doing comparisons in your code, most of the times, it is better to try not to use brackets { } in your code, because they always require a \left and a \right to go with them, which increases byte count unnecessarily. Instead, we can utilize the sign function.

Consider a naive implementation that returns 0 if a=b, and returns 1 otherwise:
(22 bytes)

\left\{a=b:0,1\right\}

Instead of doing this, we can save 9 bytes by doing a little math instead:
(11 bytes)

sign(a-b)^2

This works because sign(x) returns -1 if x is negative, 0 if x=0, and 1 otherwise. a-b is 0 only when a=b, so sign(a-b) would be 0 only when a=b. If a does not equal b, it returns either -1 or 1. The ^2 is just to convert the -1 to a 1.

Even if we wanted to return 1 if a=b and 0 otherwise, we can still save 9 bytes by doing 1-sign(a-b)^2 instead of \left\{a=b:1,0\right\}.

Tip invalidated by fireflame241's comment.

First tip to start it off.

When doing comparisons in your code, most of the times, it is better to try not to use brackets { } in your code, because they always require a \left and a \right to go with them, which increases byte count unnecessarily. Instead, we can utilize the sign function.

Consider a naive implementation that returns 0 if a=b, and returns 1 otherwise:
(22 bytes)

\left\{a=b:0,1\right\}

Instead of doing this, we can save 9 bytes by doing a little math instead:
(11 bytes)

sign(a-b)^2

This works because sign(x) returns -1 if x is negative, 0 if x=0, and 1 otherwise. a-b is 0 only when a=b, so sign(a-b) would be 0 only when a=b. If a does not equal b, it returns either -1 or 1. The ^2 is just to convert the -1 to a 1.

Even if we wanted to return 1 if a=b and 0 otherwise, we can still save 9 bytes by doing 1-sign(a-b)^2 instead of \left\{a=b:1,0\right\}.

Edit:

In fireflame241's comment, he brought to my attention that it is not required to have the \left and \right accompanying the bracket pairs. This is true for most cases. But after some testing, there are some cases where taking out the \left and \right does break the code. Specifically, if you are using any function(e.g. total or max) in addition to these brackets, the function will not work(see example below).

Example:

Suppose you want to compare the corresponding elements of two lists, a and b, and see how many of those corresponding elements are the same. That is, something like a=[1,2,3,4] and b=[2,2,3,5] will output 2(the second and third elements of each list are the same).

Here's what someone might do, after learning that you can take out the \left and \right:

total(\{a=b:1,0\})

In theory, this should work perfectly fine, but in reality, Desmos gives an error and it doesn't work. I'm pretty sure it's because it considers l to be a function, and t, o, and a to be variables in this situation. l, o, and t are not defined, so it gives an error saying so.

In cases like this, it would be better to do:

total(1-sign(a-b)^2)

as suggested by the tip below.


First tip to start it off.

When doing comparisons in your code, most of the times, it is better to try not to use brackets { } in your code, because they always require a \left and a \right to go with them, which increases byte count unnecessarily. Instead, we can utilize the sign function.

Consider a naive implementation that returns 0 if a=b, and returns 1 otherwise:
(22 bytes)

\left\{a=b:0,1\right\}

Instead of doing this, we can save 9 bytes by doing a little math instead:
(11 bytes)

sign(a-b)^2

This works because sign(x) returns -1 if x is negative, 0 if x=0, and 1 otherwise. a-b is 0 only when a=b, so sign(a-b) would be 0 only when a=b. If a does not equal b, it returns either -1 or 1. The ^2 is just to convert the -1 to a 1.

Even if we wanted to return 1 if a=b and 0 otherwise, we can still save 9 bytes by doing 1-sign(a-b)^2 instead of \left\{a=b:1,0\right\}.

added 159 characters in body
Source Link
Aiden Chow
  • 14.2k
  • 1
  • 17
  • 55

Tip invalidated by fireflame241's comment.

First tip to start it off.

When doing comparisons in your code, most of the times, it is better to try not to use brackets { } in your code, because they always require a \left and a \right to go with them, which increases byte count unnecessarily. Instead, we can utilize the sign function.

Consider a naive implementation that returns 0 if a=b, and returns 1 otherwise:
(22 bytes)

\left\{a=b:0,1\right\}

Instead of doing this, we can save 9 bytes by doing a little math instead:
(11 bytes)

sign(a-b)^2

This works because sign(x) returns -1 if x is negative, 0 if x=0, and 1 otherwise. a-b is 0 only when a=b, so sign(a-b) would be 0 only when a=b. If a does not equal b, it returns either -1 or 1. The ^2 is just to convert the -1 to a 1.

Even if we wanted to return 1 if a=b and 0 otherwise, we can still save 79 bytes by doing 1-sign(a-b)^2 instead of \left\left\{a=b:1,0\right0\right\}.

First tip to start it off.

When doing comparisons in your code, most of the times, it is better to try not to use brackets { } in your code, because they always require a \left and a \right to go with them, which increases byte count unnecessarily. Instead, we can utilize the sign function.

Consider a naive implementation that returns 0 if a=b, and returns 1 otherwise:
(22 bytes)

\left\{a=b:0,1\right\}

Instead of doing this, we can save 9 bytes by doing a little math instead:
(11 bytes)

sign(a-b)^2

This works because sign(x) returns -1 if x is negative, 0 if x=0, and 1 otherwise. a-b is 0 only when a=b, so sign(a-b) would be 0 only when a=b. If a does not equal b, it returns either -1 or 1. The ^2 is just to convert the -1 to a 1.

Even if we wanted to return 1 if a=b and 0 otherwise, we can still save 7 bytes by doing 1-sign(a-b)^2 instead of \left{a=b:1,0\right}.

Tip invalidated by fireflame241's comment.

First tip to start it off.

When doing comparisons in your code, most of the times, it is better to try not to use brackets { } in your code, because they always require a \left and a \right to go with them, which increases byte count unnecessarily. Instead, we can utilize the sign function.

Consider a naive implementation that returns 0 if a=b, and returns 1 otherwise:
(22 bytes)

\left\{a=b:0,1\right\}

Instead of doing this, we can save 9 bytes by doing a little math instead:
(11 bytes)

sign(a-b)^2

This works because sign(x) returns -1 if x is negative, 0 if x=0, and 1 otherwise. a-b is 0 only when a=b, so sign(a-b) would be 0 only when a=b. If a does not equal b, it returns either -1 or 1. The ^2 is just to convert the -1 to a 1.

Even if we wanted to return 1 if a=b and 0 otherwise, we can still save 9 bytes by doing 1-sign(a-b)^2 instead of \left\{a=b:1,0\right\}.

added 2 characters in body
Source Link
Aiden Chow
  • 14.2k
  • 1
  • 17
  • 55
Loading
Source Link
Aiden Chow
  • 14.2k
  • 1
  • 17
  • 55
Loading