Calculate Quaternion Inverse [closed] - quaternions

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Hi i'm trying to figure out how to calculate the inverse of a quaternion. A code example would be awesome.
Cheers

See Wikipedia article for the entire Quaternion math.
Don't know what language you want to use but I'll try to give some hints in Haskell.
data Quaternion = Q Double Double Double Double deriving (Show, Eq)
First, you need to implement multiplication and addition of quaternions.
instance Num Quaternion where
(+) = q_plus
(*) = q_mult
--....
q_plus (Q a b c d) (Q a' b' c' d') = Q (a + a') (b + b') (c + c') (d + d')
q_mult (Q a b c d) (Q a' b' c' d') = Q a'' b'' c'' d''
where
a'' = a * a' - b * b' - c * c' - d * d'
b'' = a * b' + b * a' + c * d' - d * c'
c'' = a * c' - b * d' + c * a' + d * b'
d'' = a * d' + b * c' - c * b' + d * a'
Multiplication with scalar should be done via a conversion:
scalar_to_q a = Q a 0 0 0
Define
i = Q 0 1 0 0
j = Q 0 0 1 0
k = Q 0 0 0 1
Then implement the conjugate and modulus:
q_conjugate q = (scalar_to_q (negate .5)) * (q + i * q * i + j * q * j + k * q * k)
q_modulus q = sqrt $ q * (q_conjugate q)
Now, the inverse:
q_inverse q = (q_conjugate q) * (scalar_to_q (m * m))
where
m = q_modulus q
Hope it's useful.
PS: the instance definition above will simplify things a little if completed successfully. I let you fill in the gaps.

Look at the Matrix and Quaternion FAQ. There are some code samples as well.

Related

IBM DB2: SQL subtraction doesn't work in denominator

I am working with manufacturing costs and the value of the fabrication still left to go (the "Net Work In Process"). The SQL is straight arithmetic but the query doesn't result in a value if there is a minus sign (subtraction) in the denominator. The database columns:
A = Material issued cost
B = Miscellaneous cost adds
C = Labor
D = Overhead
E = Setup cost
F = Scrap cost
G = Received cost (cost of assemblies completed already)
H = Original Quantity ordered
I = Quantity deviation
J = Quantity split from order
K = Quantity received (number of assemblies completed already)
The Net WIP cost is nothing more than the total cost remaining divided by the total quantity remaining. So in short, I'm simply trying to do this:
select (A + B + C + D + E - F - G) / (H + I - J - K) from MyTable
The subtractions work fine in the numerator but as soon as I subtract in the denominator, the query simply returns no value (blank). I've tried stuff like this:
select (A + B + C + D + E - F - G) / (H + I - (J + K)) from MyTable
select (A + B + C + D + E - F - G) / (H + I + (-J) + (-K)) from MyTable
select (A + B + C + D + E - F - G) / (H + I + (J * -1) + (K * -1)) from MyTable
None of these work. Just curious if anyone has come across this on IBM's DB2 database?
Thanks.
If you are returning "blank" in a numeric calculation, then you have a NULL value somewhere. Try using coalesce():
nullif(coalesce(H, 0) + coalesce(I, 0) - coalesce(J, 0) - coalesce(K, 0), 0)
You have nulls in one of the columns H, I, J, or K. Search for the offending rows using:
select H, I, J, K
from MyTable
where H is null
or I is null
or J is null
or K is null;
Then, you can treat those special cases according you your own logic. Typically you'll replace those nulls with zeroes or other values using COALESCE().
Thanks all for your comments. I did scour the columns for NULLs and there aren't any. There are then plenty of conditions where, let's say, the factory sets the order to 10 and completes (receives) 10 with no splits and no deviation. In that case:
H + I + J + K = 0 (+10 +0 -0 -10) = 0
and I can't divide by zero. So I have a different workaround for that and thanks for everyone's help.

Time complexity for the following loop of code?

I just need someone to explain one line of code for me, I don't really understand.
* this is just pseudocode
m: = 1, l := 0, s:= 0:
while m <= n do
for j = n-m to n do :
l:= l + 1
od
for j = 1 to [log n] do
s: = s +1
od
m := 3m
od
I understand the second for loop is log n time, the while loop is log base 3 n time but I am confused on the first for loop? Can someone explain, is that just o(n)? What does j = n-m really do?
The first for loop runs from n-m to n. So it has m iterations. In the last iteration of the while loop m is basically n so it runs linear in n. But the overall complexity is better than O(n log n)
In total you have this many iterations
(1 + log n) + (3 + log n) + (9 + log n) + ... + (n + log n) ( log_3 n terms )
= log_3 n * log n + (1 + 3 + 9 + ... + n) ( n can be written as 3^(log_3 n) )
= log_3 n * log n + (3^0 + 3^1 + 3^2 + ... + 3^(log_3 n)) (see this as a base 3 number. 11 is smaller than 100)
<= log_3 n * log n + 3^(1 + log_3 n)
= log_3 n * log n + 3 * n
So the dominating term is n and thus the overall complexity is O(n).

How to write a formula using sql recursion

using SQL recursion, I am trying to get a formula from a given set of numbers.
Example, if only 1 number x is provided, the output should be x.
if 2 numbers x and y are provided, then output should be x + y + xy
if 3 numbers are provide, output should be x + y + z + xy + yz + zx + xyz
excerpt:
thanks.
Priya
I'm not 100% sure what you want the result set to really look like. But, you can do this using a recursive CTE:
with vals as (
select *
from (values ('x'), ('y'), ('z')) v(a)
),
cte as (
select convert(varchar(max), a) as a, convert(varchar(max), a) as max_a
from vals
union all
select convert(varchar(max), cte.a + vals.a) as a, convert(varchar(max), vals.a)
from cte join
vals
on vals.a > cte.max_a
)
select cte.a
from cte;
This produces the expressions on each row

Nested conditions in sql

I have the where condition in the sql:
WHERE
( Spectrum.access.dim_member.centene_ind = 0 )
AND
(
Spectrum.access.Client_List_Groups.Group_Name IN ( 'Centene Health Plan Book of Business' )
AND
Spectrum.access.dim_member.referral_route IN ( 'Claims Data' )
AND
***(
Spectrum.access.fact_task_metrics.task = 'Conduct IHA'
AND
Spectrum.access.fact_task_metrics.created_by_name <> 'BMU, BMU'
AND
Spectrum.access.fact_task_metrics.created_date BETWEEN '01/01/2015 00:0:0' AND '06/30/2015 00:0:0'
)***
AND
***(
Spectrum.access.fact_outreach_metrics.outreach_type IN ( 'Conduct IHA' )
AND
(
Spectrum.dbo.ufnTruncDate(Spectrum.access.fact_outreach_metrics.metric_date) >= Spectrum.access.fact_task_metrics.metric_date
OR
Spectrum.access.fact_outreach_metrics.metric_date >= Spectrum.access.fact_task_metrics.created_date
)
)***
AND
Spectrum.access.fact_outreach_metrics.episode_seq = 1
AND
Spectrum.access.dim_member.reinstated_date Is Null
)
I have marked two of the conditions in the above code.
The 1st condition have 2 AND operators.
The 2nd condition has an AND and an OR operator.
Question 1: Does removing the outer brackets "(" in the 1st condition impact the results?
Question 2: Does removing the outer brackets "(" in the 2nd condition impact the results?
After removing the outer bracket the filters will look like:
Spectrum.access.dim_member.referral_route IN ( 'Claims Data' )
AND
Spectrum.access.fact_task_metrics.task = 'Conduct IHA'
AND
Spectrum.access.fact_task_metrics.created_by_name <> 'BMU, BMU'
AND
Spectrum.access.fact_task_metrics.created_date BETWEEN '01/01/2015 00:0:0' AND '06/30/2015 00:0:0'
AND
Spectrum.access.fact_outreach_metrics.outreach_type IN ( 'Conduct IHA' )
AND
(
Spectrum.dbo.ufnTruncDate(Spectrum.access.fact_outreach_metrics.metric_date) >= Spectrum.access.fact_task_metrics.metric_date
OR
Spectrum.access.fact_outreach_metrics.metric_date >= Spectrum.access.fact_task_metrics.created_date
)
AND
Spectrum.access.fact_outreach_metrics.episode_seq = 1
Appreciate your help.
Regards,
Jude
Order of operations dictate that AND will be processed before OR when these expressions are evaluated within a parenthesis set.
WHERE (A AND B) OR (C AND D)
Is equivalent to:
WHERE A AND B OR C AND D
But the example below:
WHERE (A OR B) AND (C OR D)
Is not equivalent to:
WHERE A OR B AND C OR D
Which really becomes:
WHERE A OR (B AND C) OR D
Technically, you should be able to safely remove the parenthesis in question for both of your examples. With the AND statement, you are adding all of your conditions together to be one large condition. When using the OR clause, you should carefully place the parenthesis so that the groups are properly segmented.
Take the following examples into consideration:
a) where y = 1 AND n = 2 AND x = 3 or x = 5
b) where y = 1 AND n = 2 AND (x = 3 or x = 5)
c) where (y = 1 AND n = 2 AND x = 3) or x = 5
In example A, the intended outcome is unclear.
In example B, the intended outcome states that all of the conditions must be met and X can be either 3 or 5.
In example C, the intended outcome states that either Y=1, N=2 and X=3 OR x=5. As long as X = 5, it doesn't matter what Y and N equal.

Theta Notation for N to the Power of Log Manipulation

In Asymptotic Notations for Order of Growth; Is the form
Theta(N ^ ( ( LOGb( a / b) + 1 ) ) )
Equivalent to
Theta(N ^ (LOGb( a ) ) ) ??
Where LOGb(a) means LOG a to base b.
Since log(a/b) = log a - log b and LOGb(b) = 1, we have LOGb(a/b)-1 = LOGb(a) - 1 + 1 = LOGb(a). No mention of asymptotics necessary, this equality is exact for all a, b > 0.