Quaternion square root
$begingroup$
Background
Quaternion is a number system that extends complex numbers. A quaternion has the following form
$$ a + bi + cj + dk $$
where $ a,b,c,d $ are real numbers and $ i,j,k $ are three fundamental quaternion units. The units have the following properties:
$$ i^2 = j^2 = k^2 = -1 $$
$$ ij = k, jk = i, ki = j $$
$$ ji = -k, kj = -i, ik = -j $$
Note that quaternion multiplication is not commutative.
Task
Given a non-real quaternion, compute at least one of its square roots.
How?
According to this Math.SE answer, we can express any non-real quaternion in the following form:
$$ q = a + bvecu $$
where $ a,b$ are real numbers and $ vecu $ is the imaginary unit vector in the form $ xi + yj + zk $ with $ x^2 + y^2 + z^2 = 1 $. Any such $ vecu $ has the property $ vecu^2 = -1 $, so it can be viewed as the imaginary unit.
Then the square of $ q $ looks like this:
$$ q^2 = (a^2 - b^2) + 2abvecu $$
Inversely, given a quaternion $ q' = x + yvecu $, we can find the square root of $ q' $ by solving the following equations
$$ x = a^2 - b^2, y = 2ab $$
which is identical to the process of finding the square root of a complex number.
Note that a negative real number has infinitely many quaternion square roots, but a non-real quaternion has only two square roots.
Input and output
Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice. Non-real means that at least one of $ b,c,d $ is non-zero.
Output is one or two quaternions which, when squared, are equal to the input.
Test cases
Input (a, b, c, d) => Output (a, b, c, d) rounded to 6 digits
0.0, 1.0, 0.0, 0.0 => 0.707107, 0.707107, 0.000000, 0.000000
1.0, 1.0, 0.0, 0.0 => 1.098684, 0.455090, 0.000000, 0.000000
1.0, -1.0, 1.0, 0.0 => 1.168771, -0.427800, 0.427800, 0.000000
2.0, 0.0, -2.0, -1.0 => 1.581139, 0.000000, -0.632456, -0.316228
1.0, 1.0, 1.0, 1.0 => 1.224745, 0.408248, 0.408248, 0.408248
0.1, 0.2, 0.3, 0.4 => 0.569088, 0.175720, 0.263580, 0.351439
99.0, 0.0, 0.0, 0.1 => 9.949876, 0.000000, 0.000000, 0.005025
Generated using this Python script. Only one of the two correct answers is specified for each test case; the other is all four values negated.
Scoring & winning criterion
Standard code-golf rules apply. The shortest program or function in bytes in each language wins.
code-golf math complex-numbers
$endgroup$
add a comment |
$begingroup$
Background
Quaternion is a number system that extends complex numbers. A quaternion has the following form
$$ a + bi + cj + dk $$
where $ a,b,c,d $ are real numbers and $ i,j,k $ are three fundamental quaternion units. The units have the following properties:
$$ i^2 = j^2 = k^2 = -1 $$
$$ ij = k, jk = i, ki = j $$
$$ ji = -k, kj = -i, ik = -j $$
Note that quaternion multiplication is not commutative.
Task
Given a non-real quaternion, compute at least one of its square roots.
How?
According to this Math.SE answer, we can express any non-real quaternion in the following form:
$$ q = a + bvecu $$
where $ a,b$ are real numbers and $ vecu $ is the imaginary unit vector in the form $ xi + yj + zk $ with $ x^2 + y^2 + z^2 = 1 $. Any such $ vecu $ has the property $ vecu^2 = -1 $, so it can be viewed as the imaginary unit.
Then the square of $ q $ looks like this:
$$ q^2 = (a^2 - b^2) + 2abvecu $$
Inversely, given a quaternion $ q' = x + yvecu $, we can find the square root of $ q' $ by solving the following equations
$$ x = a^2 - b^2, y = 2ab $$
which is identical to the process of finding the square root of a complex number.
Note that a negative real number has infinitely many quaternion square roots, but a non-real quaternion has only two square roots.
Input and output
Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice. Non-real means that at least one of $ b,c,d $ is non-zero.
Output is one or two quaternions which, when squared, are equal to the input.
Test cases
Input (a, b, c, d) => Output (a, b, c, d) rounded to 6 digits
0.0, 1.0, 0.0, 0.0 => 0.707107, 0.707107, 0.000000, 0.000000
1.0, 1.0, 0.0, 0.0 => 1.098684, 0.455090, 0.000000, 0.000000
1.0, -1.0, 1.0, 0.0 => 1.168771, -0.427800, 0.427800, 0.000000
2.0, 0.0, -2.0, -1.0 => 1.581139, 0.000000, -0.632456, -0.316228
1.0, 1.0, 1.0, 1.0 => 1.224745, 0.408248, 0.408248, 0.408248
0.1, 0.2, 0.3, 0.4 => 0.569088, 0.175720, 0.263580, 0.351439
99.0, 0.0, 0.0, 0.1 => 9.949876, 0.000000, 0.000000, 0.005025
Generated using this Python script. Only one of the two correct answers is specified for each test case; the other is all four values negated.
Scoring & winning criterion
Standard code-golf rules apply. The shortest program or function in bytes in each language wins.
code-golf math complex-numbers
$endgroup$
$begingroup$
Can we take the quaternion asa, (b, c, d)
?
$endgroup$
– nwellnhof
Nov 16 '18 at 13:19
$begingroup$
@nwellnhof Sure. Even something likea,[b,[c,[d]]]
is fine, if you can somehow save bytes with it :)
$endgroup$
– Bubbler
Nov 16 '18 at 13:50
add a comment |
$begingroup$
Background
Quaternion is a number system that extends complex numbers. A quaternion has the following form
$$ a + bi + cj + dk $$
where $ a,b,c,d $ are real numbers and $ i,j,k $ are three fundamental quaternion units. The units have the following properties:
$$ i^2 = j^2 = k^2 = -1 $$
$$ ij = k, jk = i, ki = j $$
$$ ji = -k, kj = -i, ik = -j $$
Note that quaternion multiplication is not commutative.
Task
Given a non-real quaternion, compute at least one of its square roots.
How?
According to this Math.SE answer, we can express any non-real quaternion in the following form:
$$ q = a + bvecu $$
where $ a,b$ are real numbers and $ vecu $ is the imaginary unit vector in the form $ xi + yj + zk $ with $ x^2 + y^2 + z^2 = 1 $. Any such $ vecu $ has the property $ vecu^2 = -1 $, so it can be viewed as the imaginary unit.
Then the square of $ q $ looks like this:
$$ q^2 = (a^2 - b^2) + 2abvecu $$
Inversely, given a quaternion $ q' = x + yvecu $, we can find the square root of $ q' $ by solving the following equations
$$ x = a^2 - b^2, y = 2ab $$
which is identical to the process of finding the square root of a complex number.
Note that a negative real number has infinitely many quaternion square roots, but a non-real quaternion has only two square roots.
Input and output
Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice. Non-real means that at least one of $ b,c,d $ is non-zero.
Output is one or two quaternions which, when squared, are equal to the input.
Test cases
Input (a, b, c, d) => Output (a, b, c, d) rounded to 6 digits
0.0, 1.0, 0.0, 0.0 => 0.707107, 0.707107, 0.000000, 0.000000
1.0, 1.0, 0.0, 0.0 => 1.098684, 0.455090, 0.000000, 0.000000
1.0, -1.0, 1.0, 0.0 => 1.168771, -0.427800, 0.427800, 0.000000
2.0, 0.0, -2.0, -1.0 => 1.581139, 0.000000, -0.632456, -0.316228
1.0, 1.0, 1.0, 1.0 => 1.224745, 0.408248, 0.408248, 0.408248
0.1, 0.2, 0.3, 0.4 => 0.569088, 0.175720, 0.263580, 0.351439
99.0, 0.0, 0.0, 0.1 => 9.949876, 0.000000, 0.000000, 0.005025
Generated using this Python script. Only one of the two correct answers is specified for each test case; the other is all four values negated.
Scoring & winning criterion
Standard code-golf rules apply. The shortest program or function in bytes in each language wins.
code-golf math complex-numbers
$endgroup$
Background
Quaternion is a number system that extends complex numbers. A quaternion has the following form
$$ a + bi + cj + dk $$
where $ a,b,c,d $ are real numbers and $ i,j,k $ are three fundamental quaternion units. The units have the following properties:
$$ i^2 = j^2 = k^2 = -1 $$
$$ ij = k, jk = i, ki = j $$
$$ ji = -k, kj = -i, ik = -j $$
Note that quaternion multiplication is not commutative.
Task
Given a non-real quaternion, compute at least one of its square roots.
How?
According to this Math.SE answer, we can express any non-real quaternion in the following form:
$$ q = a + bvecu $$
where $ a,b$ are real numbers and $ vecu $ is the imaginary unit vector in the form $ xi + yj + zk $ with $ x^2 + y^2 + z^2 = 1 $. Any such $ vecu $ has the property $ vecu^2 = -1 $, so it can be viewed as the imaginary unit.
Then the square of $ q $ looks like this:
$$ q^2 = (a^2 - b^2) + 2abvecu $$
Inversely, given a quaternion $ q' = x + yvecu $, we can find the square root of $ q' $ by solving the following equations
$$ x = a^2 - b^2, y = 2ab $$
which is identical to the process of finding the square root of a complex number.
Note that a negative real number has infinitely many quaternion square roots, but a non-real quaternion has only two square roots.
Input and output
Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice. Non-real means that at least one of $ b,c,d $ is non-zero.
Output is one or two quaternions which, when squared, are equal to the input.
Test cases
Input (a, b, c, d) => Output (a, b, c, d) rounded to 6 digits
0.0, 1.0, 0.0, 0.0 => 0.707107, 0.707107, 0.000000, 0.000000
1.0, 1.0, 0.0, 0.0 => 1.098684, 0.455090, 0.000000, 0.000000
1.0, -1.0, 1.0, 0.0 => 1.168771, -0.427800, 0.427800, 0.000000
2.0, 0.0, -2.0, -1.0 => 1.581139, 0.000000, -0.632456, -0.316228
1.0, 1.0, 1.0, 1.0 => 1.224745, 0.408248, 0.408248, 0.408248
0.1, 0.2, 0.3, 0.4 => 0.569088, 0.175720, 0.263580, 0.351439
99.0, 0.0, 0.0, 0.1 => 9.949876, 0.000000, 0.000000, 0.005025
Generated using this Python script. Only one of the two correct answers is specified for each test case; the other is all four values negated.
Scoring & winning criterion
Standard code-golf rules apply. The shortest program or function in bytes in each language wins.
code-golf math complex-numbers
code-golf math complex-numbers
edited Nov 16 '18 at 13:51
Bubbler
asked Nov 15 '18 at 22:54
BubblerBubbler
6,497959
6,497959
$begingroup$
Can we take the quaternion asa, (b, c, d)
?
$endgroup$
– nwellnhof
Nov 16 '18 at 13:19
$begingroup$
@nwellnhof Sure. Even something likea,[b,[c,[d]]]
is fine, if you can somehow save bytes with it :)
$endgroup$
– Bubbler
Nov 16 '18 at 13:50
add a comment |
$begingroup$
Can we take the quaternion asa, (b, c, d)
?
$endgroup$
– nwellnhof
Nov 16 '18 at 13:19
$begingroup$
@nwellnhof Sure. Even something likea,[b,[c,[d]]]
is fine, if you can somehow save bytes with it :)
$endgroup$
– Bubbler
Nov 16 '18 at 13:50
$begingroup$
Can we take the quaternion as
a, (b, c, d)
?$endgroup$
– nwellnhof
Nov 16 '18 at 13:19
$begingroup$
Can we take the quaternion as
a, (b, c, d)
?$endgroup$
– nwellnhof
Nov 16 '18 at 13:19
$begingroup$
@nwellnhof Sure. Even something like
a,[b,[c,[d]]]
is fine, if you can somehow save bytes with it :)$endgroup$
– Bubbler
Nov 16 '18 at 13:50
$begingroup$
@nwellnhof Sure. Even something like
a,[b,[c,[d]]]
is fine, if you can somehow save bytes with it :)$endgroup$
– Bubbler
Nov 16 '18 at 13:50
add a comment |
11 Answers
11
active
oldest
votes
$begingroup$
APL (NARS), 2 bytes
√
NARS has built-in support for quaternions. ¯_(⍨)_/¯
$endgroup$
4
$begingroup$
I can't help it: you should include " ¯_(ツ)_/¯ " In your answer
$endgroup$
– Barranka
Nov 16 '18 at 5:22
7
$begingroup$
You dropped this
$endgroup$
– Andrew
Nov 16 '18 at 8:33
$begingroup$
@Barranka Done.
$endgroup$
– Adám
Nov 16 '18 at 9:08
$begingroup$
@Andrew blame it on the Android app... Thank you for picking it up :)
$endgroup$
– Barranka
Nov 16 '18 at 14:31
2
$begingroup$
It'd be better if it's¯_(⍨)√¯
$endgroup$
– Zacharý
Nov 16 '18 at 21:03
add a comment |
$begingroup$
Python 2, 72 bytes
def f(a,b,c,d):s=((a+(a*a+b*b+c*c+d*d)**.5)*2)**.5;print s/2,b/s,c/s,d/s
Try it online!
More or less a raw formula. I thought I could use list comprehensions to loop over b,c,d
, but this seems to be longer. Python is really hurt here by a lack of vector operations, in particular scaling and norm.
Python 3, 77 bytes
def f(a,*l):r=a+sum(x*x for x in[a,*l])**.5;return[x/(r*2)**.5for x in[r,*l]]
Try it online!
Solving the quadratic directly was also shorter than using Python's complex-number square root to solve it like in the problem statement.
$endgroup$
$begingroup$
"Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice." So you can consider it to be a pandas series or numpy array. Series have scaling with simple multiplication, and there are various ways to get norm, such as(s*s).sum()**.5
.
$endgroup$
– Acccumulation
Nov 16 '18 at 20:04
add a comment |
$begingroup$
Wolfram Language (Mathematica), 19 bytes
Sqrt
<<Quaternions`
Try it online!
Mathematica has Quaternion built-in too, but is more verbose.
Although built-ins look cool, do upvote solutions that don't use built-ins too! I don't want votes on questions reaching HNQ be skewed.
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 55 53 bytes
Based on the direct formula used by xnor.
Takes input as an array.
q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)
Try it online!
How?
Given an array $q=[a,b,c,d]$, this computes:
$$x=sqrtfraca+sqrta^2+b^2+c^2+d^22$$
And returns:
$$left[x,fracb2x,fracc2x,fracd2xright]$$
q => // q = input array
q.map(v => // for each value v in q:
1 / q ? // if q is numeric (2nd to 4th iteration):
v / 2 / q // yield v / 2q
: // else (1st iteration, with v = a):
q = ( // compute x (as defined above) and store it in q
(v + Math.hypot(...q)) // we use Math.hypot(...q) to compute:
/ 2 // (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
) ** .5 // yield x
) // end of map()
$endgroup$
add a comment |
$begingroup$
Haskell, 51 bytes
f(a:l)|r<-a+sqrt(sum$(^2)<$>a:l)=(/sqrt(r*2))<$>r:l
Try it online!
A direct formula. The main trick to express the real part of the output as r/sqrt(r*2)
to parallel the imaginary part expression, which saves a few bytes over:
54 bytes
f(a:l)|s<-sqrt$2*(a+sqrt(sum$(^2)<$>a:l))=s/2:map(/s)l
Try it online!
$endgroup$
add a comment |
$begingroup$
Charcoal, 32 bytes
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η≧∕ηθ§≔θ⁰⊘ηIθ
Try it online! Link is to verbose version of code. Port of @xnor's Python answer. Explanation:
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η
Square all of the elements of the input and take the sum, then take the square root. This calculates $ | x + yvecu | = sqrt x^2 + y^2 = sqrt (a^2 - b^2)^2 + (2ab)^2 = a^2 + b^2 $. Adding $ x $ gives $ 2a^2 $ which is then doubled and square rooted to give $ 2a $.
≧∕ηθ
Because $ y = 2ab $, calculate $ b $ by dividing by $ 2a $.
§≔θ⁰⊘η
Set the first element of the array (i.e. the real part) to half of $ 2a $.
Iθ
Cast the values to string and implicitly print.
$endgroup$
add a comment |
$begingroup$
Java 8, 84 bytes
(a,b,c,d)->(a=Math.sqrt(2*(a+Math.sqrt(a*a+b*b+c*c+d*d))))/2+" "+b/a+" "+c/a+" "+d/a
Port of @xnor's Python 2 answer.
Try it online.
Explanation:
(a,b,c,d)-> // Method with four double parameters and String return-type
(a= // Change `a` to:
Math.sqrt( // The square root of:
2* // Two times:
(a+ // `a` plus,
Math.sqrt( // the square-root of:
a*a // `a` squared,
+b*b // `b` squared,
+c*c // `c` squared,
+d*d)))) // And `d` squared summed together
/2 // Then return this modified `a` divided by 2
+" "+b/a // `b` divided by the modified `a`
+" "+c/a // `c` divided by the modified `a`
+" "+d/a // And `d` divided by the modified `a`, with space delimiters
$endgroup$
add a comment |
$begingroup$
05AB1E, 14 bytes
nOtsн+·t©/¦®;š
Port of @xnor's Python 2 answer.
Try it online or verify all test cases.
Explanation:
n # Square each number in the (implicit) input-list
O # Sum them
t # Take the square-root of that
sн+ # Add the first item of the input-list
· # Double it
t # Take the square-root of it
© # Store it in the register (without popping)
/ # Divide each value in the (implicit) input with it
¦ # Remove the first item
®; # Push the value from the register again, and halve it
š # Prepend it to the list (and output implicitly)
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 28 bytes
s=#+Norm@##,##2/(2s)^.5&
Port of @xnor's Python 2 answer.
Try it online!
$endgroup$
add a comment |
$begingroup$
C# .NET, 88 bytes
(a,b,c,d)=>((a=System.Math.Sqrt(2*(a+System.Math.Sqrt(a*a+b*b+c*c+d*d))))/2,b/a,c/a,d/a)
Port of my Java 8 answer, but returns a Tuple instead of a String. I thought that would have been shorter, but unfortunately the Math.Sqrt
require a System
-import in C# .NET, ending up at 4 bytes longer instead of 10 bytes shorter.. >.>
The lambda declaration looks pretty funny, though:
System.Func<double, double, double, double, (double, double, double, double)> f =
Try it online.
$endgroup$
add a comment |
$begingroup$
Perl 6, 49 bytes
;(*+@^b>>².sum**.5*i).sqrt.&.re,(@b X/2*.re)
Try it online!
Curried function taking input as f(b,c,d)(a)
. Returns quaternion as a,(b,c,d)
.
Explanation
; # Block returning WhateverCode
@^b>>².sum**.5 # Compute B of quaternion written as q = a + B*u
# (length of vector (b,c,d))
(*+ *i) # Complex number a + B*i
.sqrt # Square root of complex number
.& # Return
.re, # Real part of square root
(@b X/2*.re) # b,c,d divided by 2* real part
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176048%2fquaternion-square-root%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
APL (NARS), 2 bytes
√
NARS has built-in support for quaternions. ¯_(⍨)_/¯
$endgroup$
4
$begingroup$
I can't help it: you should include " ¯_(ツ)_/¯ " In your answer
$endgroup$
– Barranka
Nov 16 '18 at 5:22
7
$begingroup$
You dropped this
$endgroup$
– Andrew
Nov 16 '18 at 8:33
$begingroup$
@Barranka Done.
$endgroup$
– Adám
Nov 16 '18 at 9:08
$begingroup$
@Andrew blame it on the Android app... Thank you for picking it up :)
$endgroup$
– Barranka
Nov 16 '18 at 14:31
2
$begingroup$
It'd be better if it's¯_(⍨)√¯
$endgroup$
– Zacharý
Nov 16 '18 at 21:03
add a comment |
$begingroup$
APL (NARS), 2 bytes
√
NARS has built-in support for quaternions. ¯_(⍨)_/¯
$endgroup$
4
$begingroup$
I can't help it: you should include " ¯_(ツ)_/¯ " In your answer
$endgroup$
– Barranka
Nov 16 '18 at 5:22
7
$begingroup$
You dropped this
$endgroup$
– Andrew
Nov 16 '18 at 8:33
$begingroup$
@Barranka Done.
$endgroup$
– Adám
Nov 16 '18 at 9:08
$begingroup$
@Andrew blame it on the Android app... Thank you for picking it up :)
$endgroup$
– Barranka
Nov 16 '18 at 14:31
2
$begingroup$
It'd be better if it's¯_(⍨)√¯
$endgroup$
– Zacharý
Nov 16 '18 at 21:03
add a comment |
$begingroup$
APL (NARS), 2 bytes
√
NARS has built-in support for quaternions. ¯_(⍨)_/¯
$endgroup$
APL (NARS), 2 bytes
√
NARS has built-in support for quaternions. ¯_(⍨)_/¯
edited Nov 16 '18 at 9:08
answered Nov 15 '18 at 23:08
AdámAdám
28.7k276206
28.7k276206
4
$begingroup$
I can't help it: you should include " ¯_(ツ)_/¯ " In your answer
$endgroup$
– Barranka
Nov 16 '18 at 5:22
7
$begingroup$
You dropped this
$endgroup$
– Andrew
Nov 16 '18 at 8:33
$begingroup$
@Barranka Done.
$endgroup$
– Adám
Nov 16 '18 at 9:08
$begingroup$
@Andrew blame it on the Android app... Thank you for picking it up :)
$endgroup$
– Barranka
Nov 16 '18 at 14:31
2
$begingroup$
It'd be better if it's¯_(⍨)√¯
$endgroup$
– Zacharý
Nov 16 '18 at 21:03
add a comment |
4
$begingroup$
I can't help it: you should include " ¯_(ツ)_/¯ " In your answer
$endgroup$
– Barranka
Nov 16 '18 at 5:22
7
$begingroup$
You dropped this
$endgroup$
– Andrew
Nov 16 '18 at 8:33
$begingroup$
@Barranka Done.
$endgroup$
– Adám
Nov 16 '18 at 9:08
$begingroup$
@Andrew blame it on the Android app... Thank you for picking it up :)
$endgroup$
– Barranka
Nov 16 '18 at 14:31
2
$begingroup$
It'd be better if it's¯_(⍨)√¯
$endgroup$
– Zacharý
Nov 16 '18 at 21:03
4
4
$begingroup$
I can't help it: you should include " ¯_(ツ)_/¯ " In your answer
$endgroup$
– Barranka
Nov 16 '18 at 5:22
$begingroup$
I can't help it: you should include " ¯_(ツ)_/¯ " In your answer
$endgroup$
– Barranka
Nov 16 '18 at 5:22
7
7
$begingroup$
You dropped this
$endgroup$
– Andrew
Nov 16 '18 at 8:33
$begingroup$
You dropped this
$endgroup$
– Andrew
Nov 16 '18 at 8:33
$begingroup$
@Barranka Done.
$endgroup$
– Adám
Nov 16 '18 at 9:08
$begingroup$
@Barranka Done.
$endgroup$
– Adám
Nov 16 '18 at 9:08
$begingroup$
@Andrew blame it on the Android app... Thank you for picking it up :)
$endgroup$
– Barranka
Nov 16 '18 at 14:31
$begingroup$
@Andrew blame it on the Android app... Thank you for picking it up :)
$endgroup$
– Barranka
Nov 16 '18 at 14:31
2
2
$begingroup$
It'd be better if it's
¯_(⍨)√¯
$endgroup$
– Zacharý
Nov 16 '18 at 21:03
$begingroup$
It'd be better if it's
¯_(⍨)√¯
$endgroup$
– Zacharý
Nov 16 '18 at 21:03
add a comment |
$begingroup$
Python 2, 72 bytes
def f(a,b,c,d):s=((a+(a*a+b*b+c*c+d*d)**.5)*2)**.5;print s/2,b/s,c/s,d/s
Try it online!
More or less a raw formula. I thought I could use list comprehensions to loop over b,c,d
, but this seems to be longer. Python is really hurt here by a lack of vector operations, in particular scaling and norm.
Python 3, 77 bytes
def f(a,*l):r=a+sum(x*x for x in[a,*l])**.5;return[x/(r*2)**.5for x in[r,*l]]
Try it online!
Solving the quadratic directly was also shorter than using Python's complex-number square root to solve it like in the problem statement.
$endgroup$
$begingroup$
"Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice." So you can consider it to be a pandas series or numpy array. Series have scaling with simple multiplication, and there are various ways to get norm, such as(s*s).sum()**.5
.
$endgroup$
– Acccumulation
Nov 16 '18 at 20:04
add a comment |
$begingroup$
Python 2, 72 bytes
def f(a,b,c,d):s=((a+(a*a+b*b+c*c+d*d)**.5)*2)**.5;print s/2,b/s,c/s,d/s
Try it online!
More or less a raw formula. I thought I could use list comprehensions to loop over b,c,d
, but this seems to be longer. Python is really hurt here by a lack of vector operations, in particular scaling and norm.
Python 3, 77 bytes
def f(a,*l):r=a+sum(x*x for x in[a,*l])**.5;return[x/(r*2)**.5for x in[r,*l]]
Try it online!
Solving the quadratic directly was also shorter than using Python's complex-number square root to solve it like in the problem statement.
$endgroup$
$begingroup$
"Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice." So you can consider it to be a pandas series or numpy array. Series have scaling with simple multiplication, and there are various ways to get norm, such as(s*s).sum()**.5
.
$endgroup$
– Acccumulation
Nov 16 '18 at 20:04
add a comment |
$begingroup$
Python 2, 72 bytes
def f(a,b,c,d):s=((a+(a*a+b*b+c*c+d*d)**.5)*2)**.5;print s/2,b/s,c/s,d/s
Try it online!
More or less a raw formula. I thought I could use list comprehensions to loop over b,c,d
, but this seems to be longer. Python is really hurt here by a lack of vector operations, in particular scaling and norm.
Python 3, 77 bytes
def f(a,*l):r=a+sum(x*x for x in[a,*l])**.5;return[x/(r*2)**.5for x in[r,*l]]
Try it online!
Solving the quadratic directly was also shorter than using Python's complex-number square root to solve it like in the problem statement.
$endgroup$
Python 2, 72 bytes
def f(a,b,c,d):s=((a+(a*a+b*b+c*c+d*d)**.5)*2)**.5;print s/2,b/s,c/s,d/s
Try it online!
More or less a raw formula. I thought I could use list comprehensions to loop over b,c,d
, but this seems to be longer. Python is really hurt here by a lack of vector operations, in particular scaling and norm.
Python 3, 77 bytes
def f(a,*l):r=a+sum(x*x for x in[a,*l])**.5;return[x/(r*2)**.5for x in[r,*l]]
Try it online!
Solving the quadratic directly was also shorter than using Python's complex-number square root to solve it like in the problem statement.
answered Nov 16 '18 at 0:30
xnorxnor
92.4k18188446
92.4k18188446
$begingroup$
"Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice." So you can consider it to be a pandas series or numpy array. Series have scaling with simple multiplication, and there are various ways to get norm, such as(s*s).sum()**.5
.
$endgroup$
– Acccumulation
Nov 16 '18 at 20:04
add a comment |
$begingroup$
"Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice." So you can consider it to be a pandas series or numpy array. Series have scaling with simple multiplication, and there are various ways to get norm, such as(s*s).sum()**.5
.
$endgroup$
– Acccumulation
Nov 16 '18 at 20:04
$begingroup$
"Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice." So you can consider it to be a pandas series or numpy array. Series have scaling with simple multiplication, and there are various ways to get norm, such as
(s*s).sum()**.5
.$endgroup$
– Acccumulation
Nov 16 '18 at 20:04
$begingroup$
"Input is a non-real quaternion. You can take it as four real (floating-point) numbers, in any order and structure of your choice." So you can consider it to be a pandas series or numpy array. Series have scaling with simple multiplication, and there are various ways to get norm, such as
(s*s).sum()**.5
.$endgroup$
– Acccumulation
Nov 16 '18 at 20:04
add a comment |
$begingroup$
Wolfram Language (Mathematica), 19 bytes
Sqrt
<<Quaternions`
Try it online!
Mathematica has Quaternion built-in too, but is more verbose.
Although built-ins look cool, do upvote solutions that don't use built-ins too! I don't want votes on questions reaching HNQ be skewed.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 19 bytes
Sqrt
<<Quaternions`
Try it online!
Mathematica has Quaternion built-in too, but is more verbose.
Although built-ins look cool, do upvote solutions that don't use built-ins too! I don't want votes on questions reaching HNQ be skewed.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 19 bytes
Sqrt
<<Quaternions`
Try it online!
Mathematica has Quaternion built-in too, but is more verbose.
Although built-ins look cool, do upvote solutions that don't use built-ins too! I don't want votes on questions reaching HNQ be skewed.
$endgroup$
Wolfram Language (Mathematica), 19 bytes
Sqrt
<<Quaternions`
Try it online!
Mathematica has Quaternion built-in too, but is more verbose.
Although built-ins look cool, do upvote solutions that don't use built-ins too! I don't want votes on questions reaching HNQ be skewed.
edited Nov 16 '18 at 11:54
answered Nov 16 '18 at 3:47
user202729user202729
14.1k12552
14.1k12552
add a comment |
add a comment |
$begingroup$
JavaScript (ES7), 55 53 bytes
Based on the direct formula used by xnor.
Takes input as an array.
q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)
Try it online!
How?
Given an array $q=[a,b,c,d]$, this computes:
$$x=sqrtfraca+sqrta^2+b^2+c^2+d^22$$
And returns:
$$left[x,fracb2x,fracc2x,fracd2xright]$$
q => // q = input array
q.map(v => // for each value v in q:
1 / q ? // if q is numeric (2nd to 4th iteration):
v / 2 / q // yield v / 2q
: // else (1st iteration, with v = a):
q = ( // compute x (as defined above) and store it in q
(v + Math.hypot(...q)) // we use Math.hypot(...q) to compute:
/ 2 // (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
) ** .5 // yield x
) // end of map()
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 55 53 bytes
Based on the direct formula used by xnor.
Takes input as an array.
q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)
Try it online!
How?
Given an array $q=[a,b,c,d]$, this computes:
$$x=sqrtfraca+sqrta^2+b^2+c^2+d^22$$
And returns:
$$left[x,fracb2x,fracc2x,fracd2xright]$$
q => // q = input array
q.map(v => // for each value v in q:
1 / q ? // if q is numeric (2nd to 4th iteration):
v / 2 / q // yield v / 2q
: // else (1st iteration, with v = a):
q = ( // compute x (as defined above) and store it in q
(v + Math.hypot(...q)) // we use Math.hypot(...q) to compute:
/ 2 // (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
) ** .5 // yield x
) // end of map()
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 55 53 bytes
Based on the direct formula used by xnor.
Takes input as an array.
q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)
Try it online!
How?
Given an array $q=[a,b,c,d]$, this computes:
$$x=sqrtfraca+sqrta^2+b^2+c^2+d^22$$
And returns:
$$left[x,fracb2x,fracc2x,fracd2xright]$$
q => // q = input array
q.map(v => // for each value v in q:
1 / q ? // if q is numeric (2nd to 4th iteration):
v / 2 / q // yield v / 2q
: // else (1st iteration, with v = a):
q = ( // compute x (as defined above) and store it in q
(v + Math.hypot(...q)) // we use Math.hypot(...q) to compute:
/ 2 // (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
) ** .5 // yield x
) // end of map()
$endgroup$
JavaScript (ES7), 55 53 bytes
Based on the direct formula used by xnor.
Takes input as an array.
q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)
Try it online!
How?
Given an array $q=[a,b,c,d]$, this computes:
$$x=sqrtfraca+sqrta^2+b^2+c^2+d^22$$
And returns:
$$left[x,fracb2x,fracc2x,fracd2xright]$$
q => // q = input array
q.map(v => // for each value v in q:
1 / q ? // if q is numeric (2nd to 4th iteration):
v / 2 / q // yield v / 2q
: // else (1st iteration, with v = a):
q = ( // compute x (as defined above) and store it in q
(v + Math.hypot(...q)) // we use Math.hypot(...q) to compute:
/ 2 // (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
) ** .5 // yield x
) // end of map()
edited Nov 16 '18 at 8:29
answered Nov 16 '18 at 1:25
ArnauldArnauld
79k795328
79k795328
add a comment |
add a comment |
$begingroup$
Haskell, 51 bytes
f(a:l)|r<-a+sqrt(sum$(^2)<$>a:l)=(/sqrt(r*2))<$>r:l
Try it online!
A direct formula. The main trick to express the real part of the output as r/sqrt(r*2)
to parallel the imaginary part expression, which saves a few bytes over:
54 bytes
f(a:l)|s<-sqrt$2*(a+sqrt(sum$(^2)<$>a:l))=s/2:map(/s)l
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 51 bytes
f(a:l)|r<-a+sqrt(sum$(^2)<$>a:l)=(/sqrt(r*2))<$>r:l
Try it online!
A direct formula. The main trick to express the real part of the output as r/sqrt(r*2)
to parallel the imaginary part expression, which saves a few bytes over:
54 bytes
f(a:l)|s<-sqrt$2*(a+sqrt(sum$(^2)<$>a:l))=s/2:map(/s)l
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 51 bytes
f(a:l)|r<-a+sqrt(sum$(^2)<$>a:l)=(/sqrt(r*2))<$>r:l
Try it online!
A direct formula. The main trick to express the real part of the output as r/sqrt(r*2)
to parallel the imaginary part expression, which saves a few bytes over:
54 bytes
f(a:l)|s<-sqrt$2*(a+sqrt(sum$(^2)<$>a:l))=s/2:map(/s)l
Try it online!
$endgroup$
Haskell, 51 bytes
f(a:l)|r<-a+sqrt(sum$(^2)<$>a:l)=(/sqrt(r*2))<$>r:l
Try it online!
A direct formula. The main trick to express the real part of the output as r/sqrt(r*2)
to parallel the imaginary part expression, which saves a few bytes over:
54 bytes
f(a:l)|s<-sqrt$2*(a+sqrt(sum$(^2)<$>a:l))=s/2:map(/s)l
Try it online!
answered Nov 16 '18 at 0:51
xnorxnor
92.4k18188446
92.4k18188446
add a comment |
add a comment |
$begingroup$
Charcoal, 32 bytes
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η≧∕ηθ§≔θ⁰⊘ηIθ
Try it online! Link is to verbose version of code. Port of @xnor's Python answer. Explanation:
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η
Square all of the elements of the input and take the sum, then take the square root. This calculates $ | x + yvecu | = sqrt x^2 + y^2 = sqrt (a^2 - b^2)^2 + (2ab)^2 = a^2 + b^2 $. Adding $ x $ gives $ 2a^2 $ which is then doubled and square rooted to give $ 2a $.
≧∕ηθ
Because $ y = 2ab $, calculate $ b $ by dividing by $ 2a $.
§≔θ⁰⊘η
Set the first element of the array (i.e. the real part) to half of $ 2a $.
Iθ
Cast the values to string and implicitly print.
$endgroup$
add a comment |
$begingroup$
Charcoal, 32 bytes
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η≧∕ηθ§≔θ⁰⊘ηIθ
Try it online! Link is to verbose version of code. Port of @xnor's Python answer. Explanation:
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η
Square all of the elements of the input and take the sum, then take the square root. This calculates $ | x + yvecu | = sqrt x^2 + y^2 = sqrt (a^2 - b^2)^2 + (2ab)^2 = a^2 + b^2 $. Adding $ x $ gives $ 2a^2 $ which is then doubled and square rooted to give $ 2a $.
≧∕ηθ
Because $ y = 2ab $, calculate $ b $ by dividing by $ 2a $.
§≔θ⁰⊘η
Set the first element of the array (i.e. the real part) to half of $ 2a $.
Iθ
Cast the values to string and implicitly print.
$endgroup$
add a comment |
$begingroup$
Charcoal, 32 bytes
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η≧∕ηθ§≔θ⁰⊘ηIθ
Try it online! Link is to verbose version of code. Port of @xnor's Python answer. Explanation:
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η
Square all of the elements of the input and take the sum, then take the square root. This calculates $ | x + yvecu | = sqrt x^2 + y^2 = sqrt (a^2 - b^2)^2 + (2ab)^2 = a^2 + b^2 $. Adding $ x $ gives $ 2a^2 $ which is then doubled and square rooted to give $ 2a $.
≧∕ηθ
Because $ y = 2ab $, calculate $ b $ by dividing by $ 2a $.
§≔θ⁰⊘η
Set the first element of the array (i.e. the real part) to half of $ 2a $.
Iθ
Cast the values to string and implicitly print.
$endgroup$
Charcoal, 32 bytes
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η≧∕ηθ§≔θ⁰⊘ηIθ
Try it online! Link is to verbose version of code. Port of @xnor's Python answer. Explanation:
≔X⊗⁺§θ⁰XΣEθ×ιι·⁵¦·⁵η
Square all of the elements of the input and take the sum, then take the square root. This calculates $ | x + yvecu | = sqrt x^2 + y^2 = sqrt (a^2 - b^2)^2 + (2ab)^2 = a^2 + b^2 $. Adding $ x $ gives $ 2a^2 $ which is then doubled and square rooted to give $ 2a $.
≧∕ηθ
Because $ y = 2ab $, calculate $ b $ by dividing by $ 2a $.
§≔θ⁰⊘η
Set the first element of the array (i.e. the real part) to half of $ 2a $.
Iθ
Cast the values to string and implicitly print.
answered Nov 16 '18 at 1:01
NeilNeil
81.7k745178
81.7k745178
add a comment |
add a comment |
$begingroup$
Java 8, 84 bytes
(a,b,c,d)->(a=Math.sqrt(2*(a+Math.sqrt(a*a+b*b+c*c+d*d))))/2+" "+b/a+" "+c/a+" "+d/a
Port of @xnor's Python 2 answer.
Try it online.
Explanation:
(a,b,c,d)-> // Method with four double parameters and String return-type
(a= // Change `a` to:
Math.sqrt( // The square root of:
2* // Two times:
(a+ // `a` plus,
Math.sqrt( // the square-root of:
a*a // `a` squared,
+b*b // `b` squared,
+c*c // `c` squared,
+d*d)))) // And `d` squared summed together
/2 // Then return this modified `a` divided by 2
+" "+b/a // `b` divided by the modified `a`
+" "+c/a // `c` divided by the modified `a`
+" "+d/a // And `d` divided by the modified `a`, with space delimiters
$endgroup$
add a comment |
$begingroup$
Java 8, 84 bytes
(a,b,c,d)->(a=Math.sqrt(2*(a+Math.sqrt(a*a+b*b+c*c+d*d))))/2+" "+b/a+" "+c/a+" "+d/a
Port of @xnor's Python 2 answer.
Try it online.
Explanation:
(a,b,c,d)-> // Method with four double parameters and String return-type
(a= // Change `a` to:
Math.sqrt( // The square root of:
2* // Two times:
(a+ // `a` plus,
Math.sqrt( // the square-root of:
a*a // `a` squared,
+b*b // `b` squared,
+c*c // `c` squared,
+d*d)))) // And `d` squared summed together
/2 // Then return this modified `a` divided by 2
+" "+b/a // `b` divided by the modified `a`
+" "+c/a // `c` divided by the modified `a`
+" "+d/a // And `d` divided by the modified `a`, with space delimiters
$endgroup$
add a comment |
$begingroup$
Java 8, 84 bytes
(a,b,c,d)->(a=Math.sqrt(2*(a+Math.sqrt(a*a+b*b+c*c+d*d))))/2+" "+b/a+" "+c/a+" "+d/a
Port of @xnor's Python 2 answer.
Try it online.
Explanation:
(a,b,c,d)-> // Method with four double parameters and String return-type
(a= // Change `a` to:
Math.sqrt( // The square root of:
2* // Two times:
(a+ // `a` plus,
Math.sqrt( // the square-root of:
a*a // `a` squared,
+b*b // `b` squared,
+c*c // `c` squared,
+d*d)))) // And `d` squared summed together
/2 // Then return this modified `a` divided by 2
+" "+b/a // `b` divided by the modified `a`
+" "+c/a // `c` divided by the modified `a`
+" "+d/a // And `d` divided by the modified `a`, with space delimiters
$endgroup$
Java 8, 84 bytes
(a,b,c,d)->(a=Math.sqrt(2*(a+Math.sqrt(a*a+b*b+c*c+d*d))))/2+" "+b/a+" "+c/a+" "+d/a
Port of @xnor's Python 2 answer.
Try it online.
Explanation:
(a,b,c,d)-> // Method with four double parameters and String return-type
(a= // Change `a` to:
Math.sqrt( // The square root of:
2* // Two times:
(a+ // `a` plus,
Math.sqrt( // the square-root of:
a*a // `a` squared,
+b*b // `b` squared,
+c*c // `c` squared,
+d*d)))) // And `d` squared summed together
/2 // Then return this modified `a` divided by 2
+" "+b/a // `b` divided by the modified `a`
+" "+c/a // `c` divided by the modified `a`
+" "+d/a // And `d` divided by the modified `a`, with space delimiters
edited Nov 16 '18 at 9:20
answered Nov 16 '18 at 9:06
Kevin CruijssenKevin Cruijssen
40.7k566210
40.7k566210
add a comment |
add a comment |
$begingroup$
05AB1E, 14 bytes
nOtsн+·t©/¦®;š
Port of @xnor's Python 2 answer.
Try it online or verify all test cases.
Explanation:
n # Square each number in the (implicit) input-list
O # Sum them
t # Take the square-root of that
sн+ # Add the first item of the input-list
· # Double it
t # Take the square-root of it
© # Store it in the register (without popping)
/ # Divide each value in the (implicit) input with it
¦ # Remove the first item
®; # Push the value from the register again, and halve it
š # Prepend it to the list (and output implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 14 bytes
nOtsн+·t©/¦®;š
Port of @xnor's Python 2 answer.
Try it online or verify all test cases.
Explanation:
n # Square each number in the (implicit) input-list
O # Sum them
t # Take the square-root of that
sн+ # Add the first item of the input-list
· # Double it
t # Take the square-root of it
© # Store it in the register (without popping)
/ # Divide each value in the (implicit) input with it
¦ # Remove the first item
®; # Push the value from the register again, and halve it
š # Prepend it to the list (and output implicitly)
$endgroup$
add a comment |
$begingroup$
05AB1E, 14 bytes
nOtsн+·t©/¦®;š
Port of @xnor's Python 2 answer.
Try it online or verify all test cases.
Explanation:
n # Square each number in the (implicit) input-list
O # Sum them
t # Take the square-root of that
sн+ # Add the first item of the input-list
· # Double it
t # Take the square-root of it
© # Store it in the register (without popping)
/ # Divide each value in the (implicit) input with it
¦ # Remove the first item
®; # Push the value from the register again, and halve it
š # Prepend it to the list (and output implicitly)
$endgroup$
05AB1E, 14 bytes
nOtsн+·t©/¦®;š
Port of @xnor's Python 2 answer.
Try it online or verify all test cases.
Explanation:
n # Square each number in the (implicit) input-list
O # Sum them
t # Take the square-root of that
sн+ # Add the first item of the input-list
· # Double it
t # Take the square-root of it
© # Store it in the register (without popping)
/ # Divide each value in the (implicit) input with it
¦ # Remove the first item
®; # Push the value from the register again, and halve it
š # Prepend it to the list (and output implicitly)
answered Nov 16 '18 at 9:22
Kevin CruijssenKevin Cruijssen
40.7k566210
40.7k566210
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 28 bytes
s=#+Norm@##,##2/(2s)^.5&
Port of @xnor's Python 2 answer.
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 28 bytes
s=#+Norm@##,##2/(2s)^.5&
Port of @xnor's Python 2 answer.
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 28 bytes
s=#+Norm@##,##2/(2s)^.5&
Port of @xnor's Python 2 answer.
Try it online!
$endgroup$
Wolfram Language (Mathematica), 28 bytes
s=#+Norm@##,##2/(2s)^.5&
Port of @xnor's Python 2 answer.
Try it online!
answered Nov 16 '18 at 11:45
alephalphaalephalpha
21.8k33094
21.8k33094
add a comment |
add a comment |
$begingroup$
C# .NET, 88 bytes
(a,b,c,d)=>((a=System.Math.Sqrt(2*(a+System.Math.Sqrt(a*a+b*b+c*c+d*d))))/2,b/a,c/a,d/a)
Port of my Java 8 answer, but returns a Tuple instead of a String. I thought that would have been shorter, but unfortunately the Math.Sqrt
require a System
-import in C# .NET, ending up at 4 bytes longer instead of 10 bytes shorter.. >.>
The lambda declaration looks pretty funny, though:
System.Func<double, double, double, double, (double, double, double, double)> f =
Try it online.
$endgroup$
add a comment |
$begingroup$
C# .NET, 88 bytes
(a,b,c,d)=>((a=System.Math.Sqrt(2*(a+System.Math.Sqrt(a*a+b*b+c*c+d*d))))/2,b/a,c/a,d/a)
Port of my Java 8 answer, but returns a Tuple instead of a String. I thought that would have been shorter, but unfortunately the Math.Sqrt
require a System
-import in C# .NET, ending up at 4 bytes longer instead of 10 bytes shorter.. >.>
The lambda declaration looks pretty funny, though:
System.Func<double, double, double, double, (double, double, double, double)> f =
Try it online.
$endgroup$
add a comment |
$begingroup$
C# .NET, 88 bytes
(a,b,c,d)=>((a=System.Math.Sqrt(2*(a+System.Math.Sqrt(a*a+b*b+c*c+d*d))))/2,b/a,c/a,d/a)
Port of my Java 8 answer, but returns a Tuple instead of a String. I thought that would have been shorter, but unfortunately the Math.Sqrt
require a System
-import in C# .NET, ending up at 4 bytes longer instead of 10 bytes shorter.. >.>
The lambda declaration looks pretty funny, though:
System.Func<double, double, double, double, (double, double, double, double)> f =
Try it online.
$endgroup$
C# .NET, 88 bytes
(a,b,c,d)=>((a=System.Math.Sqrt(2*(a+System.Math.Sqrt(a*a+b*b+c*c+d*d))))/2,b/a,c/a,d/a)
Port of my Java 8 answer, but returns a Tuple instead of a String. I thought that would have been shorter, but unfortunately the Math.Sqrt
require a System
-import in C# .NET, ending up at 4 bytes longer instead of 10 bytes shorter.. >.>
The lambda declaration looks pretty funny, though:
System.Func<double, double, double, double, (double, double, double, double)> f =
Try it online.
answered Nov 16 '18 at 13:11
Kevin CruijssenKevin Cruijssen
40.7k566210
40.7k566210
add a comment |
add a comment |
$begingroup$
Perl 6, 49 bytes
;(*+@^b>>².sum**.5*i).sqrt.&.re,(@b X/2*.re)
Try it online!
Curried function taking input as f(b,c,d)(a)
. Returns quaternion as a,(b,c,d)
.
Explanation
; # Block returning WhateverCode
@^b>>².sum**.5 # Compute B of quaternion written as q = a + B*u
# (length of vector (b,c,d))
(*+ *i) # Complex number a + B*i
.sqrt # Square root of complex number
.& # Return
.re, # Real part of square root
(@b X/2*.re) # b,c,d divided by 2* real part
$endgroup$
add a comment |
$begingroup$
Perl 6, 49 bytes
;(*+@^b>>².sum**.5*i).sqrt.&.re,(@b X/2*.re)
Try it online!
Curried function taking input as f(b,c,d)(a)
. Returns quaternion as a,(b,c,d)
.
Explanation
; # Block returning WhateverCode
@^b>>².sum**.5 # Compute B of quaternion written as q = a + B*u
# (length of vector (b,c,d))
(*+ *i) # Complex number a + B*i
.sqrt # Square root of complex number
.& # Return
.re, # Real part of square root
(@b X/2*.re) # b,c,d divided by 2* real part
$endgroup$
add a comment |
$begingroup$
Perl 6, 49 bytes
;(*+@^b>>².sum**.5*i).sqrt.&.re,(@b X/2*.re)
Try it online!
Curried function taking input as f(b,c,d)(a)
. Returns quaternion as a,(b,c,d)
.
Explanation
; # Block returning WhateverCode
@^b>>².sum**.5 # Compute B of quaternion written as q = a + B*u
# (length of vector (b,c,d))
(*+ *i) # Complex number a + B*i
.sqrt # Square root of complex number
.& # Return
.re, # Real part of square root
(@b X/2*.re) # b,c,d divided by 2* real part
$endgroup$
Perl 6, 49 bytes
;(*+@^b>>².sum**.5*i).sqrt.&.re,(@b X/2*.re)
Try it online!
Curried function taking input as f(b,c,d)(a)
. Returns quaternion as a,(b,c,d)
.
Explanation
; # Block returning WhateverCode
@^b>>².sum**.5 # Compute B of quaternion written as q = a + B*u
# (length of vector (b,c,d))
(*+ *i) # Complex number a + B*i
.sqrt # Square root of complex number
.& # Return
.re, # Real part of square root
(@b X/2*.re) # b,c,d divided by 2* real part
answered Nov 16 '18 at 14:23
nwellnhofnwellnhof
7,34511128
7,34511128
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176048%2fquaternion-square-root%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Can we take the quaternion as
a, (b, c, d)
?$endgroup$
– nwellnhof
Nov 16 '18 at 13:19
$begingroup$
@nwellnhof Sure. Even something like
a,[b,[c,[d]]]
is fine, if you can somehow save bytes with it :)$endgroup$
– Bubbler
Nov 16 '18 at 13:50