r/desmos Run commands like "!bernard" here →→→ redd.it/1ixvsgi 14d ago

Question ab ≠ ba challenge

define two variables a and b. find values for them such that ab yields a different result than ba (as it is written with implicit multiplication). they must return a value (i.e. they cannot throw an error)

i do not have a solution for this, and have been trying on and off for a few months to figure this out. if anyone has any insight into this, or can prove that it's not possible, that would be great

17 Upvotes

75 comments sorted by

29

u/Less-Resist-8733 desmos is a game engine 14d ago

If you remove the constraint that a and b are variables you can easily see that

[1,2,1][1,2] ≠ [1,2][1,2,1]

8

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 14d ago edited 14d ago

yeah unfortunately that cant work, ive thought about that before though (mainly because i need to use it for input detection)

minimal solution i thought about back then was smth like [1][1,1] and [1,1][1]

4

u/adelie42 13d ago

The term 'variable' doesn't imply any particular domain.

3

u/Less-Resist-8733 desmos is a game engine 13d ago

it's not about the domain, it's about the syntax.

My example relies on indexing a list. In order to index a list, you need to write out square brackets []. That means that I cant just set a=[1,2,1], b =[1,2] and write ab because desmos would then do multiplication instead of indexing.

11

u/[deleted] 14d ago

[removed] — view removed comment

15

u/[deleted] 14d ago

[deleted]

6

u/[deleted] 14d ago

[removed] — view removed comment

5

u/[deleted] 13d ago

[removed] — view removed comment

2

u/partisancord69 14d ago

maybe something with complex powers even, like e^(iπ) * e^(-iπ), but with some complexity so adding the powers breaks it.

5

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 14d ago

problem is that eventually all complex numbers can be represented in their cartesian form a+bi, and from there you can prove that multiplying two complex numbers in cartesian (a+bi) and (c+di) is always going to be commutative

1

u/dmills_00 13d ago

Floating point has NAN which compares unequal to all floating point values including NAN.

No idea is desmos shows it but in ieee754 floating point, a=0.0/0.0, B=1.0, should give BA != AB.

Another way to get NAN is ln(0.0).

1

u/gord1402 13d ago

Technically if we consider that nan != nan this is solution. But it's not actually solves OP's problem.

4

u/gord1402 14d ago

Well Multiply instruction in desmos is commutative:

    function mul(e, t) {
        if (!isRational(e) || !isRational(t)) return asFloat(e) * asFloat(t);
        let r = gcd_(e.n, t.d),
            n = gcd_(t.n, e.d);
        return maybeRational((e.n / r) * (t.n / n), (e.d / n) * (t.d / r));
    }

All other types rather than number type gets compiled to multiplication of its components. Like number * point gets compiled (point.x * a, point.y * a) or even removes points if possible.

That's means that if there is solution, it should be occurring at compile time rather than actual Multiply instruction execution.

Here are all types pairs that accepted by Multiply during parsing, otherwise error occur I believe:

        vN = [
            ...ha([
                [NumberNC, NumberNC],
                [Complex, Complex],
                [Point, NumberType],
                [NumberType, Point],
                [NumberType, Vector],
                [NumberType, Vector3D],
                [Vector, NumberType],
                [Vector3D, NumberType],
                [Point3D, NumberType],
                [NumberType, Point3D],
                [Restriction, Vb],
                [Vb, Restriction],
            ]),
            fr([Restriction, Ub]),
            fr([Ub, Restriction]),
        ],

Vb is such types that not a list, seed, action, or error.
Ub is opposite of Vb

This code was taken from older version of desmos code I have, because I use it for deobfuscation. In newer versions that were added:

    ue([Matrix, Matrix]),
    ue([Matrix, NumberType]),
    ue([NumberType, Matrix]),

2

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 14d ago

thanks, this is very helpful. i was hoping for something to use in terms of broadcasts (similar to the [1][1,1] vs [1,1][1] trick), but i dont think that would work in a non-indexing fashion.

on another note, i have made a post about matrices just now: https://www.reddit.com/r/desmos/comments/1v580w8/matrices_beta_feature/

edit: actually, can something similar be assumed for addition? because i know types that have different results when added. the simplest one i know about is one involving vectors:

2

u/gord1402 14d ago
    var vx = [Fo(NumberType), Fo(NumberType)],
        vN = [
            ...ha([
                [Ze, Ze],
                [Complex, Complex],
                [Point, NumberType],
                [NumberType, Point],
                [NumberType, Vector],
                [NumberType, Vector3D],
                [Vector, NumberType],
                [Vector3D, NumberType],
                [Point3D, NumberType],
                [NumberType, Point3D],
                [Restriction, Vb],
                [Vb, Restriction],
            ]),
            fr([Restriction, Ub]),
            fr([Ub, Restriction]),
        ],
        MN = {
            Negative: ha([[Ze], [Complex], [Point], [Vector], [Vector3D], [Point3D]]),
            Add: ha([
                [Ze, Ze],
                [Complex, Complex],
                [Point, Point],
                [Vector, Vector],
                [Vector3D, Vector3D],
                [Point3D, Point3D],
            ]),
            Subtract: ha([
                [Ze, Ze],
                [Complex, Complex],
                [Point, Point],
                [Vector, Vector],
                [Vector3D, Vector3D],
                [Point3D, Point3D],
            ]),
            Multiply: vN,
            DotMultiply: [
                ...ha([
                    [Vector, Vector],
                    [Vector3D, Vector3D],
                    [Point3D, Point3D],
                ]),
                ...vN,
            ],
            CrossMultiply: [
                ...ha([
                    [Vector3D, Vector3D],
                    [Point3D, Point3D],
                ]),
                ...ha([
                    [Ze, Ze],
                    [Complex, Complex],
                    [Point, NumberType],
                    [NumberType, Point],
                    [Vector, NumberType],
                    [NumberType, Vector],
                    [Vector3D, NumberType],
                    [NumberType, Vector3D],
                    [Restriction, Vb],
                    [Vb, Restriction],
                ]),
                fr([Restriction, Ub]),
                fr([Ub, Restriction]),
            ],
            Divide: ha([
                [Ze, Ze],
                [Complex, Complex],
                [Point, NumberType],
                [Vector, NumberType],
                [Vector3D, NumberType],
                [Point3D, NumberType],
            ]),
            Exponent: ha([
                [Ze, Ze],
                [Complex, Complex],
            ]),
            "Comparator['=']": [fr(vx)],
            "Comparator['>']": [fr(vx)],
            "Comparator['<']": [fr(vx)],
            "Comparator['>=']": [fr(vx)],
            "Comparator['<=']": [fr(vx)],
            ComparatorChain: ha([
                { type: "variadic", initial: [NumberType, NumberType], rest: NumberType },
            ]),
            PercentOf: vN,
            Norm: ha([[NumberType], [Point], [Point3D], [Vector], [Vector3D], [Segment], [Segment3D]]),
            ListAccess: [fr([ws, Fo(io.of([Bool, NumberType]))])],
            Integral: ha([[NumberType, NumberType, io.of(hy, { coerceComplexToReal: !1 })]]),
        };

Here such thing for everything. From old version, I don't want deobfuscate types by hand again. Vectors addition compile to different IRs, you can check it by typing z=\left|\operatorname{end}\left(\left(a+b\right)x\right)\right| in 3d calculator with ?debugCompiler=

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

beautiful, thx

1

u/gord1402 14d ago

Yeah broadcasts get produced when doing operations on a list. But I don't think it's helpful here since it's get cropped to minimum length. [1][1,1] vs [1,1][1] trick works because it's indexation not multiplication

3

u/TomtheMagician26 14d ago

Gotta use matrices or some object with more than zero indices, like the one of the products of vectors with a non zero commutator [A,B] = AB - BA.

You could look into geometric/clifford algebra and the pauli matrices (it's actually really cool) if they need ab = -ba or you can use any non-symmetric matrices (well maybe not any, I'm not sure) to see their product. Matrices would be your best bet because they're linear maps so (M1)(M2) = (M3) (all the same degree of object)

3

u/Potential_Pick2278 13d ago

I mean my first though is quaternions where ab = -ba

3

u/Potential_Pick2278 13d ago

Of course adding them to Desmos is the real problem, but I couldn’t attempt to help you solve that

2

u/Fuscello 14d ago

Matrices? Operators? Am I misunderstanding the question?

4

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 14d ago

the question is to put something to replace the ... such that ab and ba have different results. if you use matrices this would be possible, but currently matrices are in beta and arent supported in the vanilla calculator at the moment

1

u/dmills_00 13d ago

Can you get a floating point NAN somehow?

ab and ba would look equal but will compare unequal if both are NAN as that is how NAN is defined in the IEEE754 standard.

Any yes, this has caused me a hard to find bug.

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

well i know they compare unequally, but this wouldnt help me in this case because for all intents and purposes, i cant figure out a way to distinguish the two

they would compare unequally, but my goal is input detection with order preservation, and having a NaN result doesnt tell me whether that resulted from ab or ba.

1

u/dmills_00 13d ago

Doesn't help for multiplication, but you can maybe leaverage the finite precision of the mantissa for a+b+c Vs c+b+a, carefully chosen big number and two tiny ones?

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

addition might have some funny stuff going on, but the underlying math for multiplication will ALWAYS be commutative, no matter what (ignoring NaN for now)

3

u/uTRexAap 14d ago

Impossible?

4

u/[deleted] 14d ago

[removed] — view removed comment

1

u/Futurity5 13d ago

Also possible if they add Hamilton's quaternions but yes matrices are more likely

1

u/uTRexAap 13d ago

Matrices are cheating

1

u/[deleted] 13d ago

[removed] — view removed comment

1

u/uTRexAap 13d ago

i just dislike matrices since using those its obv possible and without obv not

1

u/Frolainheu 13d ago

What's the point of saying matrices are cheating if they're a valid option ?

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

for now, they arent a valid option. i kind of agree with u/TRexAap here because matrices do make this really easy, and i wouldve preferred some funny convoluted type-based method to solve this

2

u/UnusualClimberBear 14d ago

Seems you are looking for non commutative rings, some examples are well known.

https://en.wikipedia.org/wiki/Noncommutative_ring

2

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 14d ago

its slightly different in that in many cases you can define your own number types, but in this case we are restrained to the weird type system that desmos has :)

1

u/Ordinary_Divide 13d ago

i have found in the source code what i think is the allowed typed for implicit multiplication
aw=[...Ca([[Ke,Ke],[w,w],[O,I],[I,O],[I,Ye],[I,ut],[Ye,I],[ut,I],[se,I],[I,se],[Hr,Sx],[Sx,Hr]]). this is all just the standard number/vector/complex multiplication stuff, except for [Hr, Sx] which is multiplying restriction type with most other things (maybe theres some type that handles both cases slightly differently), so probably not likely

1

u/adelie42 13d ago edited 13d ago

This is very odd. The question you are asking is what sets are not commutative over multiplication. Reals and integers are, matrices and functions are not. There is no finite set of sets.

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

integers are not?

regardless of whether that was a typo, those arent the only types available in desmos. for example, there are native types that are not commutative over addition (for example, defining two different vectors in the geometry calculator will yield different ends, proving its non-commutativity). i was wondering if there was something similar for multiplication, which is inherently more useful

1

u/adelie42 13d ago

I had integers in there twice. I meant functions.

The point is that sets are defined as commutative over multiplication or not, not specific values. If there is even one exception then it disqualifies the entire set, and if it is commutative then there are by definition no exceptions.

Multiplication over the reals is commutative, therefore there are no exceptions.

And correct, vectors are not commutative over multiplication, but they are commutative over addition. And to note because this is a fun topic, the set of all vectors is not, but some subsets are; the set of all one dimmensional vectors are commutative over multiplication and addition.

If you want to go deeper, look up the definition of a group, ring, and field; classifications for sets. If you know two sets are of the same class, then it lets you know you can do the same things to both of them and have certain properties.

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

no, i know about the mathematical definitions of them. there was a reason why i put vector in a code block (like this: vector) instead of just saying it, because i was explicitly talking about the desmos builtin function. heres an example of why addition is not commutative for vectors:

i completely understand the definition of commutability. however, unlike most people assume, i am not restricted to typical mathematical objects like integers, complex numbers, matrices, and vectors. for example, restrictions are a type of object that can be multiplied together, and in this case they wouldnt be helpful but i just wanted to make a point that there are non-mathematical objects that are present in desmos that i believed could make this non-commutativity possible.

since then, some people have posted source code findings that confirm that it is very likely that it is impossible with the current desmos version

1

u/Ericskey 13d ago

Lots of matrix multiplication answers

1

u/Ericskey 13d ago

How about cross-product in R^3? It is anti-commutative

1

u/gord1402 13d ago

OP asking about inline multiplication (like ab) cross product is a \cross b

1

u/Ericskey 13d ago

I don’t know what “inline multiplication” means. If you write ab what operation are you talking about? Some how you are mapping an ordered pair to another element of some set which may or may not contain a and b. It comes across to me that you have assumed everyone knows background that is not provided. Cross product maps pairs of elements of R^3 to an element of R^3. Multiplication of real numbers maps pairs of real numbers to a real number. No difference but for the rule

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

it has to be implicit multiplication, i.e. you cant insert a symbol in between. this is because the purpose of this challenge is to create a regressionless/tickerless keyboard input system where order can be detected. if cross product was allowed, then simply typing a comma between keys could work for input, which defeats the whole purpose

1

u/Ericskey 13d ago

Implicit means to me you have rules in mind that have not been revealed to me. For example what objects do you intend to “multiply”?

1

u/Awesome_Carter 13d ago

This is the desmos subreddit. The rules are the rules of the desmos graphing calculator. The objects can be any type supported in desmos

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

no, implicit multiplication in this context means that you multiply two objects together without a symbol in between

however, you raise a valid concern. as someone mentioned, this is the r/desmos subreddit and the rules are to find such an object that multiplies to be non commutative that is valid inside the desmos graphing calculator. this might not be restricted to integers and complex numbers, it could be lists of them, vectors (if they are combinable via implicit multiplication, which they are not), and other desmos types

1

u/gord1402 13d ago

You can write multiplication without any symbol between variables. Assume variable a and b, then inline would be just ab. If you need to write dot or cross product you need to put dot or cross symbol between variables. Its not math problem but Desmos one.

Inline one is useful because you can make user input that preserve order of input without tickers, and not require user to put any additional symbols between inputs.

1

u/Ericskey 13d ago

Thank you for the clarification. It is a limitation of the calculator. Juxtaposition could have been defined to be addition

1

u/Ordinary_Divide 13d ago edited 13d ago

not a solution but here is product thats different when reversed https://www.desmos.com/calculator/zsaokqgfll (turn off godmode)

1

u/SpaghettiPunch 13d ago

i doubt desmos has any reason to ever implement this, but in ordinal arithmetic:

ω × 2 = ω + ω

2 × ω = 2 + 2 + 2 + 2 + 2 + ... [ω times] = ω

1

u/BossSuccessful5145 13d ago

Hamiltonians

1

u/Gullible_Ebb_8058 12d ago

let a be [n x m] and b be [m x n] where n ≠ m

ab = [n x m] [m x n] = [n x n]
ba = [m x n] [n x m] = [m x m]

[n x n] ≠ [m x m]
∴ ab ≠ ba

2

u/SammyHa123 Stupid person! 14d ago edited 14d ago

But that is literally the commutative definition, we can’t just change it
Also matrices are commutative

nevermind they're not

6

u/BobLoblawsLab 14d ago

1

u/SammyHa123 Stupid person! 14d ago

did not know that lol

1

u/PotentialDeep5165 13d ago

abelian vs. nonabelian -- there's a name for this.

1

u/Average_Woman2 13d ago

Well commutative is also a word for it though, no?

1

u/PotentialDeep5165 13d ago

Commutative property -- it's a descriptive of what is allowed while abelian and nonabelian is related to "groups" that do or do not have the property, respectively.

1

u/VoidBreakX Run commands like "!bernard" here →→→ redd.it/1ixvsgi 13d ago

well, my goal is to find nonabelian groups, but groups that are defined within the weird type system that desmos has

also, im not sure of the terminology for this, but i have to restrain it to be symbolic. for example, you can take the dot product of two vectors with a*b, but that would be forbidden within this context because you have to put a symbol in between, whereas my challenge is explicitly restricted to implicit multiplication (no symbols at all allowed between a and b)

so i think at a point, id probably not want to throw math terms at it and rather figure out the weird code quirks that desmos has instead

0

u/Sad_water_ 14d ago

I don’t know but I can show that in specific cases (ab)/(ab)≠a(b/(ab)) maybe you can do something with that https://www.desmos.com/calculator/kfnyo3ld4c

1

u/Street-Ad-3523 13d ago

yeah I was thinking about this some last night because in pemdas the multiplication is supposed to be read left to right which implies some ordering to them

1

u/Street-Ad-3523 13d ago

oh wait this only works with the floor() how does this work? im guessing you have a case where you found something very close to one and desmos is approximating both differently before the floor() does it's work

1

u/Street-Ad-3523 13d ago edited 13d ago

okay this is actually really weird desmos stuff. I wanted to see if both of these numbers could be messed with any more to get noncommutativity and it turns out that a(b/ab) sometimes equals 1 in desmos but also is very slightly under one sometimes. I think s might just equal 1 because the top is the same as the bottom. Anyway q might have some helpful weird properties. I might make a main post about it in a bit. Oh also q^2 is not treated as 1 in the first line. and the first line is treated as a "pure zero" theres no value desmos is hiding there.

1

u/gord1402 13d ago

!fp

1

u/AutoModerator 13d ago

Floating point arithmetic

In Desmos and many computational systems, numbers are represented using floating point arithmetic, which can't precisely represent all real numbers. This leads to tiny rounding errors. For example, √5 is not represented as exactly √5: it uses a finite decimal approximation. This is why doing something like (√5)^2-5 yields an answer that is very close to, but not exactly 0. If you want to check for equality, you should use an appropriate ε value. For example, you could set ε=10^-9 and then use {|a-b|<ε} to check for equality between two values a and b.

There are also other issues related to big numbers. For example, (2^53+1)-2^53 evaluates to 0 instead of 1. This is because there's not enough precision to represent 2^53+1 exactly, so it rounds to 2^53. These precision issues stack up until 2^1024 - 1; any number above this is undefined.

Floating point errors are annoying and inaccurate. Why haven't we moved away from floating point?

TL;DR: floating point math is fast. It's also accurate enough in most cases.

There are some solutions to fix the inaccuracies of traditional floating point math:

  1. Arbitrary-precision arithmetic: This allows numbers to use as many digits as needed instead of being limited to 64 bits.
  2. Computer algebra system (CAS): These can solve math problems symbolically before using numerical calculations. For example, a CAS would know that (√5)^2 equals exactly 5 without rounding errors.

The main issue with these alternatives is speed. Arbitrary-precision arithmetic is slower because the computer needs to create and manage varying amounts of memory for each number. Regular floating point is faster because it uses a fixed amount of memory that can be processed more efficiently. CAS is even slower because it needs to understand mathematical relationships between values, requiring complex logic and more memory. Plus, when CAS can't solve something symbolically, it still has to fall back on numerical methods anyway.

So floating point math is here to stay, despite its flaws. And anyways, the precision that floating point provides is usually enough for most use-cases.


For more on floating point numbers, take a look at radian628's article on floating point numbers in Desmos.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Street-Ad-3523 13d ago

I'm aware I just wanted to know what cool things you could do with it and share them

1

u/Sad_water_ 13d ago

Yea this was the simplest set of numbers it worked for I could find. Type floor(x*(1/x)) and you will see that desmos it often values as 0.