Pairwise Multiplication using Squares
Posted: Wed Jul 11, 2018 8:09 pm
Say you have a set of signals on the red wire (iron ore = -85, copper ore = 45, stone = -60, coal = -28) and a corresponding set of signals on the green wire (iron ore = -55, copper ore = -78, stone = -18, coal = -60). Adding each signal from the red wire to its corresponding signal on the green wire is trivial: Just hook both up to the input of a combinator. Doing the same with multiplication is not so straightforward.
We can hack our way to pairwise multiplication using a touch of algebra.
We start with the equation (a+b)^2 = a^2 + 2ab + b^2
Solve for ab: ab = [(a+b)^2 - a^2 - b^2]/2
Now we have something we can calculate with combinators. Use three arithmetic combinators for the squares, one that is connected to both the red and green wire for (a+b)^2, one that is only connected to red for a^2, and one that is only connected to green for b^2. Set all three to each ^ 2 output each. Connect the latter two to an arithmetic combinator set to each * -1 output each. If signal timing is important where you are using this then connect the (a+b)^2 combinator to any combinator that passes every value through (for example each * 1 output each) so it will be delayed the same number of ticks. Connect those to an arithmetic combinator to divide everything by 2. The final result will be each signal on the red wire multiplied by it's corresponding signal from the green wire.
Blueprint:
Caveat: This will only work correctly if all values are in the range -46340 to 46340, including when you add the green signal to the red signal. Anything outside that range will overflow when squared. There are some cases where overflows cancel out and you still get the right answer, but it doesn't always work that way.
If the signals on one wire all have a value of 1 then this can be used to filter signals from the other wire.
(I haven't yet figured out how to get pairwise division. If anybody has any ideas, please let me know.)
We can hack our way to pairwise multiplication using a touch of algebra.
We start with the equation (a+b)^2 = a^2 + 2ab + b^2
Solve for ab: ab = [(a+b)^2 - a^2 - b^2]/2
Now we have something we can calculate with combinators. Use three arithmetic combinators for the squares, one that is connected to both the red and green wire for (a+b)^2, one that is only connected to red for a^2, and one that is only connected to green for b^2. Set all three to each ^ 2 output each. Connect the latter two to an arithmetic combinator set to each * -1 output each. If signal timing is important where you are using this then connect the (a+b)^2 combinator to any combinator that passes every value through (for example each * 1 output each) so it will be delayed the same number of ticks. Connect those to an arithmetic combinator to divide everything by 2. The final result will be each signal on the red wire multiplied by it's corresponding signal from the green wire.
Blueprint:
Code: Select all
0eNrtlm1vgjAQgP/LfVxgEwRx/JXFEV5uegm0pBQzY/rf15bMOUQnbnEm2xdIud5L7znuuoWsbLEWxCTEW6Ccswbipy00tGRpab7JTY0QA0mswAGWVmaVCpKrCiXlbs6rjFgquQDlALECXyH21MIBZJIkYWfQLjYJa6sMhd7whSkHat5obc5MDNqiqzU25nUfajcFCcw7aeCAjloKXiYZrtI1aW2t8mE20eLCmmqM4IVEI5OD861JyFZ/2cXV7XAxzVfmYA0aM8ZWI1OTLd8BXqNIuyjgWWvyVtbtaNtK2ROw7kA2Rs88BBb7maPC+sxJ5C1Ju/SU80k8NVlfCkTWVwz6igvt1DdeBrfPDrerPV/vEP2xECf/DE3lj0ERnodiuourwoLaysVSxyI0j5qXeJTE9Dtxn19rQxEHY4vnzzWAwex6X7aAnyin8LI/e3IrbMy02Idzdx04vf7s9+AEffFpVtFp1LNDa0MoZxdO2pth+TsovXGswvNgRJc1vcmN9ryH67AIj7IYmFPzI6mfj5yQvs28d/6AjIZKQA9Je3OO9y7aDqxRNNbTbB75/tx7nES+Um9PiftC
If the signals on one wire all have a value of 1 then this can be used to filter signals from the other wire.
(I haven't yet figured out how to get pairwise division. If anybody has any ideas, please let me know.)