```python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import random

%matplotlib inline
```

## Statistical Hypothesis Testing

Hypothesis testing typically involves comparing two datasets to look for a statistical relationship between the two datasets.  In other cases, testing will involve a dataset versus an "idealized" dataset.

### Examples of hypothesis testing
1.  Is the mean height of Scandinavian men the same as the mean height of other European (non-Scandinavian) men?
2.  Are the number of males/females fined for speeding in New York "significantly different" than male/female arrests for speeding in Massachusetts in 2017?  (This is essentially asking if the number of fines is independent of gender and location.)
3.  A certain machine for manufacturing cardboard boxes is supposed to manufacture ones that are 1/5" thick.  A quality control technician in the company checks a sample of the output to test if the thickness is smaller than this desired target (meaning the boxes will be too weak).
4.  When making tea, is there is a difference in taste whether the tea is added to the cup first, or the milk is added to the cup first?  (A somewhat famous example described by Ron Fisher who helped lay the foundations of statistical hypothesis testing.  Dr. Muriel Bristol, a colleague of Fisher, claimed to be able to tell which was added first.)

### Null hypothesis and alternative hypothesis
A key idea of hypothesis testing is that we identify the "null hypothesis" and the "alterative hypothesis" that we want to test.  The null hypothesis is a statement that is assumed to be true.  The alternative hypothesis is a statement that we would accept (rejecting the null hypothesis as being true) if there is sufficient evidence from our observations that the say the differences from the null hypotheses are unlikely to be caused by random chance alone.

For some of the examples given above, the null hypothesis, $H_O$, and alternative hypothesis, $H_A$, might be stated as follows:
1. $H_O$: $\mu_S = \mu_E$ where $\mu_S$ is the mean height of Scandinavian men and $\mu_E$ is the mean height of other European (non-Scandinavian) men.

   $H_A$: $\mu_S \not= \mu_E$

2.  $H_O$:  The frequency of speeding fines is independent of gender and location.

    $H_A$:  The frequency of speeding fines is not independent of gender and/or location.

3.  $H_O$:  $\mu = 0.20$ where $\mu$ is the mean thickness of the boxes in inches.

    $H_A$:  $\mu < 0.20$

###  Assumptions about samples/observations
It is important to state what assumptions are being made about the samples (observations) being used.

**Common assumptions (about a continuous distribution)**
1.  Samples come from a normal distribution, or at least one that is symmteric.  Alternatively, the number of samples should be high enough (at least $30$) so that the "Law of Large Numbers", i.e. the Central Limit Theorem applies.
2.  Samples are independent from one another.
3.  For multiple datasets, it is typically assumed they have a common variance.  (That itself could constitute another hypothesis test.)

### Test statistic
Given the assumptions, we want to decide the right test to try and the appropriate test statistic to use.  The test statistic used can depend upon several factors, such as the sample size, assumptions on the underlyling distribution of the samples, whether the population standard deviation is known or unknown, if two datasets are being compared or one dataset vs an idealized known value, etc.  Other test statistics can be used if the test is for independence of categorical variables (e.g. gender and location in our example above).

The main idea is that the test statistic summarizes the dataset(s) into one value that can be used to (try to) distinguish the null hypothesis from the alternative hypothesis.

Common test statistic distributions:  Student's t distribution, normal distribution (using Law of Large Numbers with known variance), $\chi^2$ distribution

### Significance level
The significance level, $\alpha$, is the probability threshold below which the null hypothesis will be rejected.  What this means is the following:  Assume the null hypothesis is true, and let $T$ denote the test statistic that will be used in the test.  The significance level partitions the possible values of $T$ into regions where the null hypothesis will be rejected (the _critical region_), and those where it will not be rejected.  (E.g. for a normal distribution, the critical region will be the tail(s) of the distribution.)  The probability of the critical region is equal to $\alpha$.  Typical values for $\alpha$ are $0.05$ and $0.01$.

### Procedure of the test
Having chosen the test statistic $T$, and the significance level $\alpha$, we compute the observed value of $T$ using the samples/observations in our dataset.  Call this value $t_{obs}$.  We then check if $t_{obs}$ lies in the critical region.  If so, we reject the null hypothesis in favor of the alternative hypothesis.  If not, we cannot reject the null hypothesis.  Equivalently, the value of the test statistic corresponds to a $p$-value, which is the probability of that value $t_{obs}$ occurring, assuming that the null hypothesis is true.  If the $p$-value is smaller than or equal to the significance level, we reject the null hypothesis in favor of the alternative hypothesis.

### Errors in hypothesis testing
It is possible that we make an error in hypothesis testing, e.g. rejecting the null hypothesis incorrectly or failing
to reject the null hypothesis when it is not the truth (but the samples we have do not support rejecting it).

A common analogy used in hypothesis testing is to a criminal trial.  In most countries, a defendant on trial is presumed to be innocent (the "null hypothesis").  The evidence presented during the course of a trial is analogous to the samples taken in a statistical test.  If the evidence is sufficiently persuasive, the jury can find the defendant guilty (the "alternative hypothesis").  If the evidence is not persuasive, the defendant is found "not guilty".  It's possible, of course, that the jury's decision is wrong.

	Truth

        (Jury) Decision
        _Not Guilty_
_(Null Hypothesis)_
        _Guilty_
_(Alternative Hypothesis)_

        Not Guilty
(Accept Null Hypothesis)
        Ok
        Error
(**Type II Error**)

        Guilty
(Accept Alternative Hypothesis)
        Error
(**Type I Error**)
        Ok

In hypothesis testing, as in jury trials, we want to minimize the "conviction of an innocent person", or the incorrect rejection of the null hypothesis (the "Type I Error").  There is an asymmetry in that lowering the chances of a Type I error magnifies the chances of a Type II error occurring.

**Note: The significance level $\alpha$ is the (highest) probability of a Type I Error occurring.**

```python
#  Cardboard box samples
samples=[0.1803, 0.2160, 0.1622, 0.2277, 0.2253, 0.1809, 0.1765, 0.1861, 0.1814, 0.1698,
         0.1853, 0.2086, 0.1839, 0.1783, 0.1814, 0.1565, 0.2127, 0.1811, 0.1718, 0.2089,
         0.2067, 0.1614, 0.1690, 0.1812, 0.2172, 0.1555, 0.1623, 0.1887, 0.2069, 0.1676,
         0.1845, 0.1859, 0.1917, 0.2170, 0.1943, 0.1813, 0.2017, 0.2097, 0.1737, 0.2076]

print(len(samples))
print(np.mean(samples))
```

    40
    0.188465

Since we only have the samples in hand, with no before-hand knowledge about the variance
of the distribution, we perform a one-sample, one-sided t-test.

In this case we compute $(\bar{X}-\mu)/(s/\sqrt{n})$, where $\bar{X}$ is the sample mean, $s$ is the sample standard deviation, $n$ is the number of samples, and $\mu$ is the (assumed) mean of the distribution (from the null hypothesis).  We compare this to the Student's $t$-distribution with $n-1$ degrees of freedom.

```python
alpha = 0.05

#  Perform the t-test.
(statistic, p_value) = stats.ttest_1samp(samples, 0.20)
```

```python
#  Note that, by default, Python performs a two-sided t-test.
#  To get the one-sided test we want, we reject the null hypothessis if and only if
#  the test statistic is negative (based on the alternative hypothesis),
#  and we have p_value/2 < alpha.

print(statistic)
print(p_value/2 < alpha)
```

    -3.72303812077
    True

So in this case we reject the null hypothesis in favor of the alternative hypothesis ($\mu < 0.20$).

Alternatively, we can find the "critical value", where if the test statistic is less than this value, we reject the null hypothesis.  Since this is a one-sided test, we want the value of the test statistic where 5% of the cdf is below this value.

```python
t_var = stats.t(len(samples)-1)
t_critical = t_var.ppf(0.05)  #  "Invert" the cdf to find the critical value.

print(t_critical)
print(statistic < t_critical)
```

    -1.6848751195
    True

Since the value of the test statistic is less than this critical value, we reject the null hypothesis.

```python

```

```python

```

## Bayes' Theorem  (Introduction)

Bayes' Theorem (or Bayes' Rule) is used to describe the probability of an event, given other knowledge related to that event. It is a way to update our (probabilistic) beliefs as new information is revealed.

As a simple example, consider the case of a mother who has two children.
Assuming that it is equally likely that each child is a girl or a boy, consider these questions:

 - What is the probability that both of the mother's children are girls?
 - If I tell you that at least one of the children is a girl, what is the probability that both children are girls?
 - If I tell you that the eldest child is a girl, what is the probability that both children are girls?

These answers are, respectively, $\frac{1}{4}$, $\frac{1}{3}$, and $\frac{1}{2}$, so additional information about the mother's children changes our assessment of these probabilities.  (See [answers](#mother) at the end of this lesson if you are unfamiliar with this solution.)

### Bayesian inference
Bayesian inference is the use of Bayes' Theorem to update the probability of a hypothesis as more evidence becomes available.  Also used to infer values of parameters of probability distributions from observations.

### Applications of Bayes' Theorem and Bayesian Inference
1. Medical testing
2. Spam (email) detection
3. Weather prediction
4. Estimating parameters for probability distributions
5. Cracking the Enigma Code
6. The famous ``Monty Hall'' problem

#### An example
Suppose after a horrible week of upset stomachs, little sleep, pain, and weak knees, you go to the doctor.  After running a variety of tests, the doctor tells you that you have a life-threatening disease?  Should you be worried?

You need more information in order to decide if you should be (very) concerned or not, so asking the doctor, she tells you the following pieces of information about this disease and the testing method:
 - For people who have the disease, the test will correctly detect this 99% of the time.  (The _sensitivity_ is 99%.)
 - For people without the disease, the test will correctly conclude this 99% of the time.  (The _specificity_ is 99%.)
 - It is believed (estimated) that 0.1% of people have this disease.

Letting D denote the event that you have the disease, and P denoting the event that you test positive for having the disease, we are trying to determine this quantity:

$Pr(D \mid P)$ = the probability that you have the disease, given that you test positive for it

Before getting to the statement of Bayes' Theorem, let's solve this problem "by hand".

 - In population of 1000 people, we expect there will be one person with this disease, i.e. $1000 \times 0.001 = 1$.

 - For this one person with the disease, the test will be correct with probability $1\times 0.99 \approx 1$, so the test will correctly identify this person.

 - Out of the 999 people without the disease, the test will incorrectly test positive for (about) $999\times 0.01 \approx 10$ of them.

Thus, there are eleven people that test positive, but only one of them has the disease, so the chances that you have
the disease, given that you test positive is about $1/11 = 9\%$.

**Conclusion:**  If it was me, I would be worried enough to get tested again (by another doctor, who uses a different lab to process the test).

## Bayes' Theorem
Let A and B denote two events, where $Pr(B) > 0$.  Bayes' Theorem allows us to express $Pr(A\mid B)$ (the probability that A happens, given that B has happened) in terms of other probabilities.

$$Pr(A \mid B) = \frac{Pr(B \mid A)\cdot Pr(A)}{Pr(B)}$$

###  Some terminology and assumptions
$Pr(A \mid B)$, the conditional probability that the event $A$ happens given the evidence that $B$ has occurred, is typically called the _posterior probability_.  The quantity $P(A)$ is usually called the _prior probability_, and $Pr(B\mid A)$ is often called the _likelihood_.

Implicit in the use of Bayes' Theorem are the assumptions that the posterior probability and the likelihood are known quantities (or we have very good estimates).  If we gain additional knowledge that the posterior probability or the likelihood has somehow changed, we would use them to update $Pr(A\mid B)$ accordingly.

#### Our example
We want to compute $Pr(D\mid P)$.
To apply Bayes' Theorem we need our other probabilities, $Pr(P \mid D)$, $Pr(D)$, and $Pr(P)$.

```python
# The likelihood
p_Pos_given_Disease = 0.99

#  The prior probability
p_Disease = 0.001
```

Finding $Pr(P)$ is the slightly tricky one here.  This uses the so-called "Law of Total Probability".

```python
p_Pos = 0.001*0.99 + 0.999*0.01
```

Then we apply Bayes' Theorem.

```python
p_Disease_given_Pos = p_Pos_given_Disease * p_Disease / p_Pos

print(p_Disease_given_Pos)
```

    0.09016393442622951

### The second test
Suppose having tested positive for the disease, you go for a second test, which is positive again.

What's your estimation now of the probability you have the disease?  (See [end](#second-test-answer) for answer.)

### Improving test effectiveness
Suppose, through spending time, effort, and (most critically) money, we could improve the test in one of two ways:
 - The test could be 100% accurate for those who have the disease, i.e. 100% of those who have the disease will test positive.
 - The test could be made 99.8% accurate for those without the disease, so that a person without the disease will only test positive 0.2% of the time.

Which improvement would be better in this case?  (See [end](#effectiveness-answer) for answer.)

```python

```

### Bayesian inference

Bayes' Theorem can be used to estimate parameters of probability distributions, given an assumption about the underlying distribution.

For example, coin flipping can be represented by a Bernoulli random variable with a (possibly unknown) parameter $P$, which equals the probability of obtaining a "heads" in one flip.  Our goal might be to estimate the parameter $P$ based on observed evidence of coin flips.

Recall that repeated Bernoulli trials corresponds to the binomial distribution, giving the probability of $k$ "successes" in $n$ trials, where success happens with probability $P$.  The binomial probability mass function has this expression:
$$Pr(k;n,P) = {n \choose k}P^k(1-P)^{n-k}.$$

As stated, we want to use Bayes' Theorem to help us estimate $P$.  Bayes' Theorem tells us that

$$Pr(P=p\mid heads) \propto Pr(heads \mid P=p)\cdot Pr(P=p)$$

where "$\propto$" means "proportional to".  We know that $Pr(heads \mid P=p) = p$, so if we know (or could "guess") a possible distribution for the random variable $P$, we could update our knowledge about $P$ given new evidence of an observed coin flip.

In this case the "right" probability distribution to try for $P$ is the Beta distribution, denoted $\mathbb{B}(\alpha,\beta)$, which is a distribution on the interval $[0,1]$ with the probability density function

$$p(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha,\beta)}.$$

Here $B(\alpha,\beta)$ denotes the Beta function, which is the normalizing constant that makes this a probability distribution.

Our application of Bayes' Theorem becomes:

$$
 \begin{align}
 Pr(P=p\mid heads) & \propto\,\, Pr(heads \mid P=p)\cdot Pr(P=p) \\
                   & \propto\,\, p\cdot p^{\alpha-1}(1-p)^{\beta-1} \\
                   & \propto\,\, p^{\alpha}(1-p)^{\beta-1}.
 \end{align}
$$

It isn't too difficult to show that
$$Pr(P=p \mid heads) = \mathbb{B}(\alpha+1,\beta),$$
and similarly that
$$Pr(P=p \mid tails) = \mathbb{B}(\alpha, \beta+1).$$

In other words, if $P$ (the prior) is distributed according to the Beta distribution $\mathbb{B}(\alpha,\beta)$, then the _new_ distribution of $P$ (the posterior) is a Beta distribution that depends upon observing a "heads" or "tails" of the new coin flip, either $\mathbb{B}(\alpha+1,\beta)$ or $\mathbb{B}(\alpha,\beta+1)$, respectively.

**Terminology:** With the assumption that the prior of $P$ is the Beta distribution, and the posterior for $P$ is another Beta distribution (with different parameters) we say that the Beta distribution is a "conjugate prior" for Bernoulli trials.

```python

```

From the properties of the Beta distribution, we know that $\mathbb{E}(\mathbb{B}(\alpha,\beta))=\frac{\alpha}{\alpha+\beta}$.
Therefore, we have that our prior probability is $P=Pr(heads) = \frac{\alpha}{\alpha+\beta}$.  Observations make us readjust this belief about $P$ for every coin flip we observe.

If we observe a "heads", we want to adjust $P$ upwards, and observing "tails" makes us adjust $P$ downwards.  We do this by changing the parameters of the Beta distribution as noted above.

As the number of observations increases, our estimate for P should get better.

```python
P = 0.8

alpha = 1
beta = 1
l = []
dists = []

for k in range(101):
    estP = alpha/(alpha+beta)
    if k % 10 == 0:
        l.append([alpha, beta, round(estP,4)])
        dists.append(stats.beta(a=alpha,b=beta))
    r = random.random()
    if r <= P:
        alpha = alpha+1
    else:
        beta = beta+1

print(l)
print(round(alpha/(alpha+beta),4))
```

    [[1, 1, 0.5], [11, 1, 0.9167], [20, 2, 0.9091], [27, 5, 0.8438], [35, 7, 0.8333], [41, 11, 0.7885], [49, 13, 0.7903], [56, 16, 0.7778], [66, 16, 0.8049], [74, 18, 0.8043], [81, 21, 0.7941]]
    0.7961

```python
plt.figure()
for k, d in enumerate(dists[0::len(dists)//5]):
    ax=plt.subplot(2,3,k+1)
    xPoints = np.arange(0,1,1/200)
    ax.plot(xPoints, d.pdf(xPoints))
    ax.set_title("Beta(a={a}, b={b})".format(**d.kwds))
plt.tight_layout()
```

```python

```

####  Estimation the probability of "heads"
Suppose we are examining another coin.  What's the estimate for p, the probability of a "heads" for this coin, when we are given this list of 150 coin tosses?  (See answer [below](#coin-example-answer).)

```python
ht = ['H','T','H','H','T','T','T','H','H','T','T','T','H','T','T',
      'T','T','H','H','H','T','H','T','T','T','T','H','H','H','T',
      'T','T','H','H','T','H','T','T','H','H','H','H','T','T','H',
      'H','H','T','T','H','H','H','T','T','T','T','T','H','T','H',
      'H','T','H','T','T','T','T','T','H','T','T','T','H','T','T',
      'H','T','T','H','T','T','H','T','T','T','T','H','T','T','H',
      'T','H','H','T','H','T','T','T','H','T','T','T','T','H','H',
      'T','T','T','H','H','T','T','T','H','H','T','H','T','H','T',
      'T','T','H','T','H','T','T','T','T','T','H','T','T','T','T',
      'H','H','T','T','T','H','H','T','H','T','T','H','T','T','H']
```

```python

```

```python

```

```python

```

```python

```

### Answers to qustions asked

#### A mother's children (solution)

There are four possibilities for the mother's two children, GG, GB, BG, and BB (given in birth order).  Assuming that B and G are equally likely, then these four possibilities are also equally likely.  Hence, with no information, we see the probability that the mother has two girls is $1/4$.

If we are told that at least one child is a girl, this eliminates the BB option, leaving three equally likely options, so the probability that she has two girls is $1/3$.

Finally, if we are told that the eldest child is a girl, this leaves only the two choices $GG$ and $GB$, each equally likely, so the probability she has two girls is $1/2$.

[[Back to Introduction to Bayes' Thm](#intro)]

```python

```

#### The second test (solution)
Suppose having tested positive for the disease, you go for a second test, which is positive again.

What's your estimation now of the probability you have the disease?

The first test (effectively) alters the prior probability $Pr(D)$, from 0.1% to (about) 9%.  This isn't quite accurate, but is close enough for a good estimate.

```python
# New prior
p_Disease = 0.09

# New denominator, since the prior has been updated (given the positive test)
p_Pos = 0.09*0.99+0.91*0.01

p_Disease_given_Pos = p_Pos_given_Disease * p_Disease / p_Pos
print(p_Disease_given_Pos)
```

    0.9073319755600815

So now you're about 91% positive that you have the disease.

[[Back to "The second test"](#second-test)]

```python

```

#### Improving test effectiveness (solution)

```python
# 100% accuracy for someone with the disease
p_Disease = 0.001
p_Pos_given_Disease = 1
p_Pos = 0.001*1+0.999*0.01

p_Disease_given_Pos = p_Pos_given_Disease*p_Disease/p_Pos
print(p_Disease_given_Pos)
```

    0.09099181073703368

So only a minimal improvement to what a positive test tells you.

```python
# 99.8% accuracy for someone without the disease, but 99% for someone with the disease
p_Disease = 0.001
p_Pos_given_Disease = 0.99
p_Pos = 0.001*0.99+0.999*0.002

p_Disease_given_Pos = p_Pos_given_Disease*p_Disease/p_Pos
print(p_Disease_given_Pos)
```

    0.33132530120481923

Much better!  (Ok, not better that it's more likely that you have the disease, but better information is gained by the initial positive test.)

[[Back to "Improving test effectiveness"](#effectiveness)]

```python

```

#### Estimating the probability of "heads" (solution)

```python
ht = ['H','T','H','H','T','T','T','H','H','T','T','T','H','T','T',
      'T','T','H','H','H','T','H','T','T','T','T','H','H','H','T',
      'T','T','H','H','T','H','T','T','H','H','H','H','T','T','H',
      'H','H','T','T','H','H','H','T','T','T','T','T','H','T','H',
      'H','T','H','T','T','T','T','T','H','T','T','T','H','T','T',
      'H','T','T','H','T','T','H','T','T','T','T','H','T','T','H',
      'T','H','H','T','H','T','T','T','H','T','T','T','T','H','H',
      'T','T','T','H','H','T','T','T','H','H','T','H','T','H','T',
      'T','T','H','T','H','T','T','T','T','T','H','T','T','T','T',
      'H','H','T','T','T','H','H','T','H','T','T','H','T','T','H']
```

As before, we use the Beta distribution as a prior, and update this distribution for each "heads" or "tails" we see.  In this case, we start with $\alpha = \beta = 1$ and increase the appropriate variable for "heads" or "tails".

```python
alpha = 1 + sum(1 for x in ht if x == "H")
beta = 1 + sum(1 for x in ht if x == "T")

print(alpha, beta)
print("Estimate for p: ", alpha/(alpha+beta))
```

    60 92
    Estimate for p:  0.39473684210526316

```python

```


# Exponents, Radicals & Logarithms

```python
import math
from math import floor, ceil, log
import numpy as np
```

### Solving exponential equation
> Example:

[$$1111 = 5.2^x$$](https://www.youtube.com/watch?v=7Ig6kVZaWoU)

```python
# Using math.log
a = 1111
b = 5.20

# Find the x from above equation
x = log(1111, 5.2)

# using truncate()
def truncate(n, digit=0):
    multiplier = 10**digit
    res = math.ceil(n * multiplier)/ multiplier
    return res

# Round of up to thousandths
result = truncate(x, 3)
print('x == {} from the example problem.'.format(result))

#checking use floor to round down to whole number
check = floor(b** result)
print('5.2^x is {}.'.format(check))
print(a == check)
```

    x == 4.254 from the example problem.
    5.2^x is 1111.
    True

> if : $y = 5.2^x$ solve for y

```python
import pandas as pd

# Create a dataframe with an x column containing values from -10 to 10
df = pd.DataFrame ({'x': range(-10, 11)})
pd.set_option('display.float_format', '{:.1f}'.format)
# Add a y column by applying the slope-intercept equation to x
df['y'] = b**df['x']

#Display the dataframe
print(df)

```

         x          y
    0  -10        0.0
    1   -9        0.0
    2   -8        0.0
    3   -7        0.0
    4   -6        0.0
    5   -5        0.0
    6   -4        0.0
    7   -3        0.0
    8   -2        0.0
    9   -1        0.2
    10   0        1.0
    11   1        5.2
    12   2       27.0
    13   3      140.6
    14   4      731.2
    15   5     3802.0
    16   6    19770.6
    17   7   102807.2
    18   8   534597.3
    19   9  2779905.9
    20  10 14455510.6

```python
type(df)
```

    pandas.core.frame.DataFrame

```python
df.x
```

    0    -10
    1     -9
    2     -8
    3     -7
    4     -6
    5     -5
    6     -4
    7     -3
    8     -2
    9     -1
    10     0
    11     1
    12     2
    13     3
    14     4
    15     5
    16     6
    17     7
    18     8
    19     9
    20    10
    Name: x, dtype: int64

```python
df.y
```

    0           0.0
    1           0.0
    2           0.0
    3           0.0
    4           0.0
    5           0.0
    6           0.0
    7           0.0
    8           0.0
    9           0.2
    10          1.0
    11          5.2
    12         27.0
    13        140.6
    14        731.2
    15       3802.0
    16      19770.6
    17     102807.2
    18     534597.3
    19    2779905.9
    20   14455510.6
    Name: y, dtype: float64

```python
df.loc[df['y'] == 0], df['x']

```

    (Empty DataFrame
     Columns: [x, y]
     Index: [], 0    -10
     1     -9
     2     -8
     3     -7
     4     -6
     5     -5
     6     -4
     7     -3
     8     -2
     9     -1
     10     0
     11     1
     12     2
     13     3
     14     4
     15     5
     16     6
     17     7
     18     8
     19     9
     20    10
     Name: x, dtype: int64)

```python
#slope and y-intercept
slope, intercept = np.polyfit(df.x, df.y, 1)

```

```python
slope
```

    226898.3463522912

```python

```

```python

```

```python
# x and y intercept
# x == 0 and y == 1 or (0, 1) for y-intercept
for x, y in zip(df['x'], df['y']):
    if y == 0 or x == 0:
        print(x,y)

```

    0 1.0

```python
#Compute y-intercept
#@ index 10, x == 0; y == 1
df.loc[df['x'] == 0, 'y']
```

    10   1.0
    Name: y, dtype: float64

```python
#Compute x-intercept
# @ (-2, 0) but many zero values in y with this problem
#@ index 10, x == 0; y == 1
df.loc[df['y'] == 0], df['x']
```

    (Empty DataFrame
     Columns: [x, y]
     Index: [], 0    -10
     1     -9
     2     -8
     3     -7
     4     -6
     5     -5
     6     -4
     7     -3
     8     -2
     9     -1
     10     0
     11     1
     12     2
     13     3
     14     4
     15     5
     16     6
     17     7
     18     8
     19     9
     20    10
     Name: x, dtype: int64)

```python
xs = np.array(df.x, dtype= np.float64)
ys = np.array(df.y, dtype=np.float64)
```

```python
xs
```

    array([-10.,  -9.,  -8.,  -7.,  -6.,  -5.,  -4.,  -3.,  -2.,  -1.,   0.,
             1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

```python
ys
```

    array([6.91777709e-08, 3.59724409e-07, 1.87056692e-06, 9.72694801e-06,
           5.05801296e-05, 2.63016674e-04, 1.36768671e-03, 7.11197087e-03,
           3.69822485e-02, 1.92307692e-01, 1.00000000e+00, 5.20000000e+00,
           2.70400000e+01, 1.40608000e+02, 7.31161600e+02, 3.80204032e+03,
           1.97706097e+04, 1.02807170e+05, 5.34597285e+05, 2.77990588e+06,
           1.44555106e+07])

```python
slope, intercept = np.polyfit(xs, ys, 1)
```

```python
slope
```

    226898.3463522912

> Best-fit of slope

[\begin{equation*}
m = \frac{\overline{x} * \overline{y} - \overline{xy}}{\overline{(x)}^2 - \overline{x^2} }
\end{equation*}](https://pythonprogramming.net/how-to-program-best-fit-line-slope-machine-learning-tutorial/)

```python
intercept
```

    852252.3253232383

```python
from statistics import mean
```

```python
# The slope computation using np and user defined best fit slope have same result.
def best_fit_slope(xs,ys):
    m = (((mean(xs)*mean(ys)) - mean(xs*ys)) /
         ((mean(xs)**2) - mean(xs*xs)))
    return m

m = best_fit_slope(xs,ys)
print ('The slope is {}.'.format(m))
```

    The slope is 226898.34635229118.

```python
from scipy import stats

```

```python
slop1, intercept1, r_value, p_value, std_err = stats.linregress(xs, ys)
```

```python
slop1
```

    226898.34635229112

```python
intercept1
```

    852252.3253232383

```python
p_value
```

    0.044145300975017135

### P-value

- A small p-value (typically ≤ 0.05) indicates strong evidence against the null hypothesis, so you reject the null hypothesis.

- A large p-value (> 0.05) indicates weak evidence against the null hypothesis, so you fail to reject the null hypothesis.

- p-values very close to the cutoff (0.05) are considered to be marginal (could go either way). Always report the p-value so your readers can draw their own conclusions.

| Truth        |          Accept Ho                    |           Reject Ho                  |
|:------------:|:-------------------------------------:|:------------------------------------:|
| Ho is true   | Correct Decision P: 1-alpha           | Type 1 error P(false positive): alpha|
| Ho is false  | Type 2 error P (false negative): beta | Correct Decision P: 1-beta           |

```python
std_err
```

    105258.72700985038

```python
print('r-squared: ', r_value**2)
```

    r-squared:  0.196505875077304

```python
from matplotlib import pyplot as plt
%matplotlib inline
plt.plot(xs, ys, '--m', marker="o", label='y = 5.2^x')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('y = 5.2^x')
plt.legend(loc='upper left')
plt.axhline()
plt.axvline()
plt.grid()
plt.show()
```

#### Logarithmic Equation

```python
from sympy import *
init_printing()
```

```python
x, y = symbols('x y')
aa = Eq(x**2 - 2*x + 1)
aa
```

```python
solve(aa)
```

```python
vv = log(14, 2)
vv

```

```python
simplify(vv)
```

```python
expand(vv)
```

```python
s = log(x**2 - x - 30)
t = x**2 - x - 30
s
```

```python
solve(s)
```

```python
s1 = solve(s)
simplify(s1)
```

```python
t

```

```python
solve(t)
```

```python
q = x**2 - 4*x + 4
q
```

```python
solve(q)
```

```python

```

```python

```


```python
from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
import mshr
```

### Solve the original system for full-field solution

```python
wavelength = np.array([1.0/20, 1.0/20]);
wavenumber = 2*np.pi/wavelength
C = Expression("2.0 + sin(k1*x[0]) * cos(k2*x[1])", k1=wavenumber[0], k2=wavenumber[1], degree=2)
f = Expression("exp(-amplitude*(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2) ))", amplitude = 10, degree = 2)
# create a very fine mesh so that it the finite element solution can capture
# the fluctuation in the material parameter C
mesh = UnitSquareMesh(200, 200)
V = FunctionSpace(mesh, "Lagrange", 2)

u = TrialFunction(V)
v = TestFunction(V)
a = dot(grad(v), C * grad(u)) * dx
L = f * v * dx
# solve PDE for the full-field solution
u_ff = Function(V)
solve(a == L, u_ff, DirichletBC(V, Constant(0.0), (
    lambda x, on_boundary: (near(x[0], 0, DOLFIN_EPS_LARGE) or near(x[0], 1, DOLFIN_EPS_LARGE)) and on_boundary)))

# plot material parameter and solution
plt.figure(figsize=(10,8), dpi=80)
plt.subplot(2,2,1)
plt.colorbar(plot(project(C, V), title="Material Parameter")); plt.grid()

plt.subplot(2,2,2)
plt.colorbar(plot(project(f, V), title="External Source")); plt.grid()

plt.subplot(2,2,3)
plt.colorbar(plot(u_ff, title="Full-field solution")); plt.grid()
```

### Solve the microscopic BVP to obtain the macroscopic elastic tensor

```python
wavelength = np.array([1.0/20, 1.0/20]);
wavenumber = 2*np.pi/wavelength
vertices = np.array([[-wavelength[0]/2, -wavelength[1]/2],
                     [wavelength[0]/2, -wavelength[1]/2],
                     [wavelength[0]/2, wavelength[1]/2],
                     [-wavelength[0]/2, wavelength[1]/2]])
domain = mshr.Polygon([Point(vertices[0,:]), Point(vertices[1,:]),
                       Point(vertices[2,:]), Point(vertices[3,:])])
mesh_RVE = mshr.generate_mesh(domain, 48)

# volume of the RVE
vol = np.prod(wavelength)
print(vol)

plot(mesh_RVE)
```

```python
# class used to define the periodic boundary map
class PeriodicBoundary(SubDomain):
    # Constructor
    def __init__(self, vertices, TOL=DOLFIN_EPS_LARGE):
        super().__init__()
        self.TOL = TOL
        self.vertices = vertices
        # vectors generating periodicity
        self.a = np.zeros((2, 2))
        self.a[0,:] = vertices[1,:] - vertices[0,:]
        self.a[1,:] = vertices[3,:] - vertices[0,:]

        # vectors normal to vectors a[0,:] and a[1,:]
        self.n = np.zeros((2,2))
        self.n[0,:] = [ -self.a[0,1], self.a[0,0] ]
        self.n[1,:] = [ -self.a[1,1], self.a[1,0] ]

        assert np.dot(self.n[0,:], self.a[0,:]) < self.TOL
        assert np.dot(self.n[1,:], self.a[1,:]) < self.TOL

    def inside(self, x, on_boundary):
        # return True if x is on the left or bottom boundaries
        # AND NOT on one of the bottom-right or top-left vertices
        top_left = np.allclose(x, self.vertices[3,:], self.TOL)
        bottom_right = np.allclose(x, self.vertices[1,:], self.TOL)
        x_vector = x - self.vertices[0,:]
        bottom = (np.abs(np.dot(self.n[0,:], x_vector)) < self.TOL)
        left = (np.abs(np.dot(self.n[1,:], x_vector)) < self.TOL)

        return (left or bottom) and \
            (not (top_left or bottom_right)) and on_boundary
    # Map right boundary (R) to the left boundary (L)
    def map(self, x, y):
        # top right node
        if np.allclose(x, self.vertices[2,:]):
            y[0] = x[0] - (self.a[0, 0] + self.a[1,0])
            y[1] = x[1] - (self.a[0, 1] + self.a[1,1])
        # right boundary
        elif np.abs(np.dot(self.n[1,:], x - self.vertices[1,:])) < self.TOL:
            y[0] = x[0] - self.a[0,0]
            y[1] = x[1] - self.a[0,1]
        # top boundary
        else:
            y[0] = x[0] - self.a[1,0]
            y[1] = x[1] - self.a[1,1]
```

```python
class K(UserExpression):
    def __init__(self, k1, k2, **kargs):
        super().__init__()
        self.k1 = k1
        self.k2 = k2

    def eval_cell(self, values, x, cell):
        values[0] = 2 + np.sin(self.k1*x[0]) * np.cos(self.k2*x[1])

    def value_shape(self):
        return ()

kappa = K(wavenumber[0], wavenumber[1])
```

```python
kappa = Expression("2.0 + sin(k1*x[0]) * cos(k2*x[1])",
                   k1=wavenumber[0], k2=wavenumber[1], degree=2)
```

```python
def epsilon(u):
    return grad(u)

def sigma(u, eps_):
    return kappa * (grad(u) + eps_)
```

```python
ElemU = FiniteElement("CG", mesh_RVE.ufl_cell(), 1)
ElemL = FiniteElement("R", mesh_RVE.ufl_cell(), 0)
W = FunctionSpace(mesh_RVE, MixedElement([ElemU, ElemL]),
                 constrained_domain=PeriodicBoundary(vertices))
```

```python
u, c = TrialFunction(W)   # the fluctuation field
v, d = TestFunction(W)

MacroStrain = Constant(np.array([1.0, 0]))
F = dot(epsilon(v), kappa * (grad(u) + MacroStrain))*dx + c*v*dx + u*d*dx
a, L = lhs(F), rhs(F)
# a = dot(grad(v), kappa*(grad(u) + Constant(np.array([0, 0])))*dx + c*v*dx + u*d*dx
# L = dot(grad(v), Constant(np.array([1.0, 0])))*dx

w = Function(W)
solve(a == L, w, [])

u_fluc, lagrange_mul = w.split()
```

```python
plt.figure(figsize=(8,4.5), dpi=80)
plt.subplot(position=[0.1, 0.9, 0.3, 0.35])
plot(project(kappa, FunctionSpace(mesh_RVE, 'CG', 1)), title="RVE material parameter"); plt.grid();
plt.subplot(position=[0.6, 0.9, 0.3, 0.35])
plot(u_fluc, title="fluctuation field"); plt.grid()
plt.subplot(position=[0.1, 0.4, 0.3, 0.35])
plot(u_fluc.dx(0), title="fluctuation gradient"); plt.grid()
plt.subplot(position=[0.6, 0.4, 0.3, 0.35])
plot(sigma(u_fluc, MacroStrain), title="resultant stress field"); plt.grid()
```

Now, we compute the macroscopic tangent $\overline{\mathbf{C}}$. This tangent is a second-order tensor and its components can be computed by solving only microscopic boundary value problems. In fact, at the macroscopic we know that the resultant constitutive is linear and given by
\begin{equation}
    \overline{\mathbf{q}} = \overline{\mathbf{C}}\cdot \overline{\mathbf{p}}
\end{equation}
Therefore, if we choose $\overline{\mathbf{p}_{i}}$ to be the unit basis vectors in $\mathbb{R}^2$, then we can derive $\overline{\mathbf{q}}_{i}$ as the $i^\text{th}$-column of $\overline{\mathbf{C}}$. Indeed, let us conosider $\overline{\mathbf{p}}_1 = \begin{bmatrix} 1 \\ 0 \end{bmatrix}$. Then, it implies that
\begin{equation}
\overline{q}_i = \sum\limits_{j}\overline{C}_{ij} \overline{p}_j = C_{i\,\times}
\end{equation}

```python
I = np.eye(2)
C_Macro = np.zeros((2, 2))

w = Function(W)
for irun in range(2):
    MacroStrain = Constant(I[:,irun])

    F = dot(epsilon(v), sigma(u, MacroStrain))*dx + c*v*dx + u*d*dx
    a, L = lhs(F), rhs(F)

    solve(a == L, w, [])
    u_fluc, lagrange_mul = w.split()

    C_Macro[:, irun] = np.array([assemble(sigma(u_fluc, MacroStrain)[i]*dx)\
                     for i in range(2)]) / vol
```

```python
C_Macro
```

    array([[1.93546648e+00, 1.24506780e-06],
           [1.24506780e-06, 1.93546754e+00]])

### Solve the homogenized boundary value problem

```python
mesh_Macro = UnitSquareMesh(10, 10, "crossed")
V_Macro = FunctionSpace(mesh_Macro, "CG", 1)

bc = DirichletBC(V_Macro, 0, lambda x, on_boundary: (
    near(x[0], 0, DOLFIN_EPS_LARGE) or near(x[0], 1, DOLFIN_EPS_LARGE)) \
    and on_boundary)

u = TrialFunction(V_Macro)
v = TestFunction(V_Macro)
a = dot(grad(v), Constant(C_Macro) * grad(u)) * dx
L = f * v * dx
# solve PDE
u_homo = Function(V_Macro)
solve(a == L, u_homo, bc)
```

```python
plt.figure(figsize=(10, 8), dpi=80)

plt.subplot(2,2,1)
plt.colorbar(plot(u_homo, title="homogenized solution")); plt.grid()

plt.subplot(2,2,2)
plt.colorbar(plot(u_ff, title="full-field solution")); plt.grid()

plt.subplot(2,2,3)
plt.colorbar(plot(project(u_homo, V) - u_ff,
             title="$u_{\mathrm{homo}} - u_{\mathrm{full}}$"))
```


# Prototyping Models w/ PyTorch

## Setup

```python
%matplotlib inline
import os

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import sklearn.datasets
import sklearn.linear_model
import torch
from torch.nn import Linear, ReLU, MSELoss

from chmp.ds import mpl_set
from chmp.torch_utils.nn import linear
from chmp.torch_utils.model import Model

RUNNING_AS_TEST = os.environ.get("PYTEST_CURRENT_TEST") is not None
```

```python
# create a regression data set
x, y = sklearn.datasets.make_regression(
    n_samples=1_000,
    n_features=10,
    n_informative=2,
    shuffle=False,
    random_state=42_42_42,
)

# with redudant features
x = np.concatenate([x, x], axis=1)
n_samples, n_features = x.shape
```

```python
print(f"number of samples: {n_samples}")
print(f"number of features {n_features}")
```

    number of samples: 1000
    number of features 20

## Linear regression model with horeshoe prior

See C. Louizos, K. Ullrich, and M. Welling, "[Bayesian Compression for Deep Learning](https://arxiv.org/abs/1705.08665)" (2017).

Use Horseshoe prior on the weights:

$$
\begin{align}
    s_a \sim &\mathcal{G}(0, \tau_0^2) \\
    s_b^{-1} &\sim \mathcal{G}(0, 1/2) \\
    \alpha_{ij} &\sim \mathcal{G}(0, 1/2) \\
    \beta^{-1}_{ij} &\sim \mathcal{G}(0, 1/2) \\
    \tilde{w}_{ij} &\sim \mathcal{N}(0, 1) \\
    w_{ij} &= \tilde{w}_{ij} \sqrt{s_a / s_b} \sqrt{\alpha / \beta}
\end{align}
$$

Use the variational approximation:
$$
\begin{align}
    q(s_a) &= \mathcal{LN}(\dots) \\
    q(s_b^{-1}) &= \mathcal{LN}(\dots) \\
    q(\alpha_{ij}) &= \mathcal{LN}(\dots) \\
    q(\beta_{ij}^-{1}) &= \mathcal{LN}(\dots) \\
    q(\tilde{w}_{ij}) &= \mathcal{N}(\dots) \\
\end{align}
$$

```python
from chmp.torch_utils.bayes import WeightsHS, NormalModelConstantScale, SimpleBayesModel
```

```python
class LinearHS(NormalModelConstantScale):
    def __init__(self, in_features, out_features, tau_0, bias=True):
        super().__init__()

        self.weights = WeightsHS((in_features, out_features), tau_0=tau_0)
        self.bias = torch.nn.Parameter(torch.zeros(1)) if bias else 0

    def transform(self, x):
        return self.bias + linear(x, self.weights())

    def kl_divergence(self):
        return self.weights.kl_divergence()

def mse_loss(pred, y):
    loc, _ = pred
    return torch.mean((loc - y) ** 2.0)
```

```python
model = SimpleBayesModel(
    module=LinearHS(n_features, 1, tau_0=1e-5),
    n_observations=n_samples,
    optimizer_kwargs=dict(lr=1e-2),
)

history = model.fit(
    x, y,
    epochs=500 if not RUNNING_AS_TEST else 1,
    verbose=True,
    metrics=[mse_loss],
    validation_data=(x, y),
    dtype='float32',
)
y_pred_loc, y_pred_scale = model.predict(x.astype('float32'))

linear_coefs = sklearn.linear_model.LinearRegression().fit(x, y).coef_
weights = np.concatenate([model.module.weights().detach().numpy() for _ in range(100)], axis=1)
```

    [done. took 2m 33s] 128 mse_loss: 3.96 (499 / 500)

    /Volumes/Home/.local/share/virtualenvs/misc-exp-8XcdncOT/lib/python3.6/site-packages/sklearn/linear_model/base.py:485: RuntimeWarning: internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). Falling back to 'gelss' driver.
      linalg.lstsq(X, y)

```python
_, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(16, 4))

plt.sca(ax1)
plt.scatter(y_pred_loc, y)
mpl_set(xlabel="Prediction", ylabel="Truth")

plt.sca(ax2)

sns.violinplot(data=weights.T)
plt.plot(linear_coefs, 's', markeredgecolor='r', markerfacecolor='none')
mpl_set(xlabel="Feature", ylabel="Weight")

plt.sca(ax3)
plt.plot(history.get('epoch'), history.get('loss'))
mpl_set(xlabel="Epoch", ylabel="Loss")
```

## Attention

Use an attention model of the form

$$
\begin{align}
    \alpha_{ij} &= q(x_i)^T k(x_j) \\
    p_{ij} &= \mathrm{softmax}(\alpha_{ij}, \mathrm{mask}) \\
    \hat{y}_i &= \sum_j p_{ij} v(x_j, y_j)
\end{align}
$$

```python
from chmp.torch_utils.attention import Transformer
```

```python
def transform_x(idx):
    mask = np.ones(len(x))
    mask[idx] = 0

    return {
        'query_x': x[idx].astype('float32'),
        'mask': mask
    }

def transform_xy(idx):
    batch_x = transform_x(idx)
    batch_y = y[idx].astype('float32')
    return batch_x, batch_y
```

```python
model = Model(
    module=Transformer(
        key_module=torch.nn.Sequential(
            Linear(n_features, 20),
            ReLU(),
            Linear(20, 20),
            ReLU(),
        ),
        search_x=x.astype('float32'),
        search_y=y.astype('float32'),
    ),
    loss=MSELoss(),
    optimizer='Adam',
    optimizer_kwargs=dict(lr=1e-2),
)

history = model.fit_transformed(
    transform_xy,
    range(len(x)),
    batch_size=20,
    dtype=None,
    epochs=10 if not RUNNING_AS_TEST else 1,
    verbose=True,
)
y_pred = model.predict_transformed(transform_x, range(len(x)), batch_size=20, dtype=None, verbose=True)
```

    [done. took 4.95s] 11.1 (9 / 10)

    [done. took 0.25s] predict

```python
_, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))

plt.sca(ax1)
plt.scatter(y_pred, y)
mpl_set(xlabel="Prediction", ylabel="Truth")

plt.sca(ax2)
plt.plot(history.get('epoch'), history.get('loss'))
mpl_set(xlabel="Epoch", ylabel="Loss")
```

```python

```


```python
from sympy import *

var('alpha phi theta A B M N Z G R')

#A=Matrix([[Mul(cos(alpha),cos(phi)),Mul(sin(alpha),cos(phi)),-sin(phi)],[-sin(alpha),cos(alpha),0],[Mul(cos(alpha),sin(phi)),Mul(sin(alpha),sin(phi)),cos(phi)]])
Z=Matrix([[cos(alpha)*cos(phi),sin(alpha)*cos(phi),-sin(phi)],[-sin(alpha),cos(alpha),0],[cos(alpha)*sin(phi),sin(alpha)*sin(phi),cos(phi)]])

G=Matrix([[cos(theta),-sin(theta),0],[sin(theta),cos(theta),0],[0,0,1]])

R=(Z.T).multiply(G.multiply(Z))
#print_latex(N)
print_latex(R.applyfunc(simplify))
R.applyfunc(simplify)

```

    \left[\begin{matrix}\sin^{2}{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} \cos{\left(\theta \right)} & - \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} - \sin{\left(\theta \right)} \cos{\left(\phi \right)} & \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} - \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)}\\- \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} + \sin{\left(\theta \right)} \cos{\left(\phi \right)} & - \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \cos{\left(\theta \right)} & \left(- \sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \cos{\left(\phi \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)}\\\left(- \sin{\left(\alpha \right)} \sin{\left(\theta \right)} - \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)} & \left(- \sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} & \sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\end{matrix}\right]

$\displaystyle \left[\begin{matrix}\sin^{2}{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} \cos{\left(\theta \right)} & - \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} - \sin{\left(\theta \right)} \cos{\left(\phi \right)} & \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} - \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)}\\- \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)} + \sin{\left(\theta \right)} \cos{\left(\phi \right)} & - \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \cos{\left(\theta \right)} & \left(- \sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \cos{\left(\phi \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)}\\\left(- \sin{\left(\alpha \right)} \sin{\left(\theta \right)} - \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)} & \left(- \sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} & \sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\end{matrix}\right]$

```python
((R[1,0]-R[0,1])/(2*sin(theta))).simplify()
```

$\displaystyle \cos{\left(\phi \right)}$

```python
((R[2,1]-R[1,2])/(2*sin(theta))).simplify()
```

$\displaystyle \sin{\left(\phi \right)} \cos{\left(\alpha \right)}$

```python
Z.inv()
```

$\displaystyle \left[\begin{matrix}\frac{\cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & - \frac{\sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & \frac{\sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}}\\\frac{\sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & \frac{\cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & \frac{\sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}}\\- \frac{\sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} & 0 & \frac{\cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\end{matrix}\right]$

```python
Z.T.inv()
```

$\displaystyle \left[\begin{matrix}\frac{\cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & \frac{\sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & - \frac{\sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\\- \frac{\sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & \frac{\cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & 0\\\frac{\sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & \frac{\sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & \frac{\cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\end{matrix}\right]$

```python
(Z.T.inv()*R*Z.inv()).applyfunc(simplify)
```

$\displaystyle \left[\begin{matrix}\cos{\left(\theta \right)} & - \sin{\left(\theta \right)} & 0\\\sin{\left(\theta \right)} & \cos{\left(\theta \right)} & 0\\0 & 0 & 1\end{matrix}\right]$

```python
(Z.inv()*R*Z.T.inv())
```

$\displaystyle \left[\begin{matrix}\frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(- \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} - \sin{\left(\phi \right)} \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(- \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} - \sin{\left(\phi \right)} \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & - \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(- \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} - \sin{\left(\phi \right)} \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\\\frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(- \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} - \sin{\left(\phi \right)} \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(- \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} - \sin{\left(\phi \right)} \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} & - \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)} + \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\alpha \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(- \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)} - \sin{\left(\phi \right)} \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\\\frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} - \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \sin{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \sin{\left(\phi \right)} \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \sin{\left(\alpha \right)} \cos{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin{\left(\phi \right)} \cos{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} \cos{\left(\phi \right)} + \cos{\left(\alpha \right)} \cos{\left(\theta \right)}\right) \sin{\left(\alpha \right)} + \left(\sin{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} - \sin{\left(\theta \right)} \cos{\left(\alpha \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} + \sin{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} \cos{\left(\alpha \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \cos{\left(\alpha \right)}}{\sin^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \sin{\left(\alpha \right)} \sin{\left(\phi \right)}}{\sin^{2}{\left(\alpha \right)} \sin^{2}{\left(\phi \right)} + \sin^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)} + \cos^{2}{\left(\alpha \right)} \cos^{2}{\left(\phi \right)}} & - \frac{\left(\frac{\left(- \left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \sin{\left(\phi \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(\left(\sin{\left(\alpha \right)} \sin{\left(\theta \right)} + \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)}\right) \cos{\left(\alpha \right)} \cos{\left(\phi \right)} - \left(- \sin{\left(\alpha \right)} \cos{\left(\theta \right)} + \sin{\left(\theta \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\alpha \right)} + \sin^{2}{\left(\phi \right)} \cos^{2}{\left(\alpha \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} + \frac{\left(\frac{\left(\sin^{2}{\left(\phi \right)} \cos{\left(\theta \right)} + \cos^{2}{\left(\phi \right)}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}} - \frac{\left(\sin{\left(\alpha \right)} \sin{\left(\phi \right)} \sin{\left(\theta \right)} - \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)} \cos{\left(\theta \right)} + \sin{\left(\phi \right)} \cos{\left(\alpha \right)} \cos{\left(\phi \right)}\right) \sin{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\right) \cos{\left(\phi \right)}}{\sin^{2}{\left(\phi \right)} + \cos^{2}{\left(\phi \right)}}\end{matrix}\right]$