# ideas by u/DaVinci103 # MIT License, Copyright (c) 2024 Marvin Borner :import std/Combinator . :import std/Pair . :import std/Math/Real R ι (+0.0r) : (+1.0r) # adds two complex numbers add &[[&[[(R.add 3 1) : (R.add 2 0)]]]] ⧗ Complex → Complex → Complex # subtracts two complex numbers sub &[[&[[(R.sub 3 1) : (R.sub 2 0)]]]] ⧗ Complex → Complex → Complex # multiplies two complex numbers mul &[[&[[p : q]]]] ⧗ Complex → Complex → Complex p R.sub (R.mul 3 1) (R.mul 2 0) q R.add (R.mul 3 0) (R.mul 2 1) # divides two complex numbers div &[[&[[p : q]]]] ⧗ Complex → Complex → Complex p R.div (R.add (R.mul 3 1) (R.mul 2 0)) (R.add (R.mul 1 1) (R.mul 0 0)) q R.div (R.sub (R.mul 2 1) (R.mul 1 0)) (R.add (R.mul 1 1) (R.mul 0 0)) # negates a complex number negate &[[(R.negate 1) : (R.negate 0)]] ⧗ Complex → Complex # inverts a complex number invert &[[p : q]] p R.div 1 (R.add (R.mul 1 1) (R.mul 0 0)) q R.div 0 (R.add (R.mul 1 1) (R.mul 0 0))