Search This Blog

Solving systems of linear equations with SymPy

SymPy is a great Python library for symbolic computation. It can be used e.g. for solving systems of linear equations. So if you are manually solving problems from a linear algebra book, and you want to verify your solutions, you can check them against the solutions that SymPy provides. You just need to know how to code the linear system for SymPy.

Here is an example which illustrates this very well.

import sympy as sp

x, y, z, t = sp.symbols(['x', 'y', 'z', 't'])

solution = solve(

      [

          2 * x + 3 * y -  5*z + 1 * t  -  2  , 

          2 * x + 3 * y -  1*z + 3 * t  -  8  , 

          6 * x + 9 * y -  7*z + 7 * t  - 18  , 

          4 * x + 6 * y - 12*z + 1 * t  -  1   

      ]

      , 

      [x,y,z,t]

)

print(solution)


The code above solves the following system.

$$\begin{bmatrix}2 & 3 & -5 & 1 \\2 & 3 & -1 & 3 \\6 & 9 & -7 & 7 \\4 & 6 & -12 & 1 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ t \end{bmatrix} = \begin{bmatrix} 2 \\ 8 \\ 18 \\ 1 \end{bmatrix} $$

The solution which SymPy provides is this one 

{z: 3/2 - t/2, x: -7*t/4 - 3*y/2 + 19/4}

The form of this solution implies that $y$ and $t$ are free variables, and can thus be given any values. The system is then solved for the leading variables which happen to be $x$ and $z$.


Solving the integrals $\int \frac{ \alpha\sin{x} + \beta\cos{x} }{a\sin{x} + b\cos{x}} dx$

I found this as a problem in a book, I solved it, and I kind of liked it. 

So I am going to share the end result here.

The solution to the integral 

$$\int \frac{ \alpha\sin{x} + \beta\cos{x} }{a\sin{x} + b\cos{x}} dx$$ 

is the antiderivative function 

$$F(x) = Ax + B \ln{\left|a\sin{x} + b\cos{x}\right|}$$  

where 

$$A = \frac{\alpha a + \beta b}{a^2+b^2}$$

$$B = \frac{\beta a - \alpha b}{a^2+b^2}$$

One can verify this by differentiating $F(x)$.

The original problem, the one which I encountered was actually asking to find the constants $A$ and $B$.


Indefinite integrals from a differential binomial

The expression 

$$x^{m}(ax^n + b)^p \tag{1}$$

where $m,n,p \in \mathbb{Q}$ and $a,b$ are real non-zero constants is called a differential binomial

The integrals from a differential binomial have the form

$$\int x^{m}(ax^n + b)^p dx \tag{2} $$

They can be solved in elementary functions in three cases (detailed below). In all other cases these integrals cannot be solved in elementary functions. Here are the 3 cases in which the integral $(2)$ is solvable. 

1) $p \in \mathbb{Z}$

In this case we take $k = LCM(m,n)$ and we apply the substitution $x=t^k$, this leads us to an integral from a rational function of $t$.

2) $\frac{m+1}{n} \in \mathbb{Z}$

In this case to solve the integral we apply the substitution: $ax^n + b = t$. In this case another (rather obvious) second substitution may be needed $t = g(u)$ to arrive at a rational function of $u$.

3) $\frac{m+1}{n} + p \in \mathbb{Z}$

In this case to solve the integral we apply the substitution: $a + bx^{-n} = t$. In this case another (rather obvious) second substitution may be needed $t = g(u)$ to arrive at a rational function of $u$.


IPython commands

IPython basic commands

1) ? after module / object / method name - show help info

2) ?? after module / object / method name - show detailed help info (usually the source code)

3) <TAB> after object or while typing object/method - auto-completion

4) <TAB> when importing - auto-completion 

Examples: 

from <module> import <TAB>

from itertools import co<TAB>


IPython keyboard shortcuts 

1) Navigation shortcuts: 

Ctrl-a - Move cursor to the beginning of the line

Ctrl-e - Move cursor to the end of the line

Ctrl-b (or the left arrow key) - Move cursor back one character

Ctrl-f (or the right arrow key) - Move cursor forward one character


2) Text manipulation shortcuts: 

Backspace key - Delete previous character in line

Ctrl-d - Delete next character in line

Ctrl-k - Cut text from cursor to end of line

Ctrl-u - Cut text from beginning of line to cursor

Ctrl-y - Yank (i.e., paste) text that was previously cut

Ctrl-t - Transpose (i.e., switch) previous two characters


3) Command history shortcuts: 

Ctrl-p (or the up arrow key) - Access previous command in history

Ctrl-n (or the down arrow key) - Access next command in history

Ctrl-r - Reverse-search through command history


4) Miscellaneous Shortcuts:

Ctrl-l - Clear terminal screen

Ctrl-c - Interrupt current Python command

Ctrl-d - Exit IPython session


IPython magic commands - more info can be found here

IPython general information - see here


An interesting identity involving radicals

This identity came out while solving the indefinite integral

$$I = \int \frac{dx}{(x+1)\sqrt{x^2+x+1}} \tag{1} $$

I got my answer as 

$$I = F(x) = \ln { \frac{-1 + \sqrt{x^2+x+1}}{-1-2x+\sqrt{x^2+x+1}}} \tag{2}$$

but the answer given in the book was 

$$I = G(x) = \ln {\frac{x + \sqrt{x^2+x+1}}{x + 2 + \sqrt{x^2+x+1}}} \tag{3}$$

Checking the two answers with WA shows that both are correct.

So it is natural then to ask... what is the relation between these two expressions?

After some short struggle, I found that the relation is as follows:

$$ \frac{-1 + \sqrt{x^2+x+1}}{-1-2x+\sqrt{x^2+x+1}}= (-1) \cdot \frac{x + \sqrt{x^2+x+1}}{x + 2 + \sqrt{x^2+x+1}} \tag{4}$$

One can easily prove this by letting $a = \sqrt{x^2+x+1}$ and then doing some simple algebraic manipulations.

Of course $(4)$ is true only for those real values of $x$ for which both sides are well-defined. 

The curious thing is that even though $F(x)$ and $G(x)$ have identical derivatives (identical when viewed as an expression of $x$, I mean), they are never simultaneously well-defined. Why? Because when $$f(x) = \frac{-1 + \sqrt{x^2+x+1}}{-1-2x+\sqrt{x^2+x+1}}$$ and $$g(x) = \frac{x + \sqrt{x^2+x+1}}{x+2+\sqrt{x^2+x+1}}$$ are both defined and non-zero, they have opposite signs (as $(4)$ shows). So we can take logarithm either from one or the other but not from both at the same time.


Euler substitutions

Euler substitutions are used for solving integrals of the form

$$\int R(x, \sqrt{ax^2+bx+c}) dx \tag{1} $$ where $R$ is a rational two-argument function.

There is plenty of information about them on the web so this post will be just very short.

1) The first Euler substitution is defined by

$\sqrt{ax^2+bx+c} = \sqrt{a} \cdot x + t \tag{2}$

It is used when $a \gt 0$

2) The second Euler substitution is defined by

$\sqrt{ax^2+bx+c} = x \cdot t + \sqrt{c} \tag{3}$

It is used when $c \gt 0$

3) The third Euler substitution is used when the quadratic polynomial 

$ax^2+bx+c$ has 2 distinct real roots $\alpha$ and $\beta$.

It is defined by the equality

$\sqrt{a(x-\alpha)(x-\beta)} = t \cdot (x-\alpha) \tag{4}$

The equality $(2), (3),$ or $(4)$ is then solved for $x$, and $x$ is replaced in $(1)$ with the respective resulting expression/function of $t$. This allows us to transform the integral $(1)$ into an integral from a rational function of $t$.


Several indefinite integrals by non-trivial substitutions

This post comes to show several indefinite integrals which can be solved by non-trivial substitutions.

$$\int \frac{dx}{(a^2+x^2)^2} = \frac{1}{4a^3} \cdot \left(2 \cdot \arctan \frac{x}{a} + \sin \left(2 \cdot \arctan \frac{x}{a}\right)\right), \ \ \ where \ a \gt 0  \tag{1}$$ 

$$\int \frac{x^3 dx}{(a^2+x^2)^3} = \frac{1}{4a^2} \cdot \sin^4\left(\arctan \frac{x}{a}\right), \ \ \ where \ a \gt 0  \tag{2}$$ 

The above two integrals (1) and (2) can be solved by using the substitution $x = a \cdot \tan t$. 

This solution uses the fact that the tangent function is a well-known monotonic bijection from $(-\frac{\pi}{2}, \frac{\pi}{2})$ to $\mathbb{R}$.

Below are two more integrals which are solved by a different substitution.

$$\int \sqrt{a^2+x^2} dx = \frac{a^2}{2} \cdot \left(\ln\left(\frac{x}{a} + \sqrt{\frac{x^2}{a^2} + 1}\right) + \frac{x}{a} \cdot \sqrt{\frac{x^2}{a^2} + 1}\right), \ \ \ where \ a \gt 0  \tag{3}$$ 

$$\int \frac{dx} {(a^2+x^2)^\frac{3}{2}} = \frac{1}{a^2} \cdot \frac{x^2 + x \cdot \sqrt{x^2+a^2}}{x^2  + a^2 + x \cdot \sqrt{x^2+a^2}}, \ \ \ where \ a \gt 0  \tag{4}$$  

The integrals (3) and (4) can be solved by the substitution $x = a \cdot \sinh{t}$. 
This solution uses the fact that the hyperbolic sine function is a well-known monotonic bijection from $\mathbb{R}$ to $\mathbb{R}$.



Solving the integrals $\int \sin^mx \cdot \cos^nx \cdot dx$

In this post we look at solving the integrals of the form 


$$I(m,n) = \int \sin^mx \cdot \cos^nx \cdot dx \tag{1}$$

where $m,\ n$ are whole numbers (not necessarily positive).

This is usually done by applying some of the recurrence formulas listed below.

$$I(m,n) = \frac{1}{m+1} \sin^{m+1}x \cos^{n-1}x + \frac{n-1}{m+1} \cdot I(m+2, n-2), for\ \ m \ne -1 \tag{2}$$

$$I(m,n) = -\frac{1}{n+1} \sin^{m-1}x \cos^{n+1}x + \frac{m-1}{n+1} \cdot I(m-2, n+2), for\ \ n \ne -1 \tag{3}$$

$$I(m,n) = I(m-2,n) - I(m-2,n+2), for\ any \ m,n \tag{4}$$

$$I(m,n) = I(m,n-2) - I(m+2,n-2), for\ any \ m,n \tag{5}$$

Then from (2) and (5) we derive (2'), while from (3) and (4) we derive (3'). The formulas (2') and (3') are used to reduce the degree either of the sine or of the cosine. They turn out to be very useful when $m,n$ are positive.

$$I(m,n) = \frac{1}{m+n} \sin^{m+1}x\cos^{n-1}x + \frac{n-1}{m+n} \cdot I(m, n-2),\ for \ m \ne -1,\ m+n \ne 0 \tag{2'}$$

$$I(m,n) = -\frac{1}{m+n} \sin^{m-1}x\cos^{n+1}x + \frac{m-1}{m+n} \cdot I(m-2, n),\ for \ n \ne -1,\ m+n \ne 0 \tag{3'}$$

When both $m, n$ are non-negative usually it is useful to apply formula (6). Note though that (6) is valid for any values of $m,n$, and not just for non-negative values. 

$$I(m,n) = I(m+2, n) + I(m, n+2) \tag{6}$$ 

The last two formulas (2'') and (3'') can be easily derived from (2') and (3'). They allow us to increase the degree of the sine or of the cosine. They are usually used when $m$ or $n$ is negative. 

$$I(m,n) =  -\frac{1}{n+1} \sin^{m+1}x\cos^{n+1}x +  \frac{m+n+2}{n+1} \cdot I(m, n+2),\ for \ m \ne -1,\ n \ne -1 \tag{2''}$$

$$I(m,n) =  \frac{1}{m+1} \sin^{m+1}x\cos^{n+1}x +  \frac{m+n+2}{m+1} \cdot I(m+2, n),\ for \ m \ne -1,\ n \ne -1 \tag{3''}$$

It can be proved that using these formulas the calculation of the integral (1) always boils down to calculating one of the below given integrals. These integrals below are calculated directly (e.g. by integration by parts or by even simpler means). 

$$\int \sin{x}\ dx = -\cos{x}$$

$$\int \cos{x}\ dx = \sin{x}$$

$$\int \frac{dx}{\sin{x}} = \ln |\tan ({\frac{x}{2})}|$$

$$\int \frac{dx}{\cos{x}} = \ln |\tan {(\frac{x}{2}+\frac{\pi}{4})}|$$

$$\int \sin{x}\ \cos{x}\ dx = -\frac{1}{4}\cos{2x}$$

$$\int \frac{dx}{\sin{x}\cos{x}} = \ln |\tan {(x)}|$$

$$\int \frac{\sin{x}}{\cos{x}}\ dx = - \ln |\cos{x}|$$

$$\int \frac{\cos{x}}{\sin{x}}\ dx = \ln |\sin{x}|$$

$$\int \sin^2{x}\ dx = \frac{1}{2}x - \frac{1}{4}\sin{2x}$$

$$\int \cos^2{x}\ dx = \frac{1}{2}x + \frac{1}{4}\sin{2x}$$

$$\int \frac{dx}{\sin^2{x}} = -\cot{x}$$

$$\int \frac{dx}{\cos^2{x}} = \tan{x}$$

It should be noted that when $m$ is odd we can introduce/enter one of the $\sin{x}$ multipliers under the differential sign (where it becomes $\cos{x}$). Then by letting $t=\cos{x}$ and by using that $\sin^2{x} = 1 - \cos^2{x}$, we get an integral from a rational function of $t$.

Analogically, when $n$ is odd we can introduce/enter one of the $\cos{x}$ multipliers under the differential sign (where it becomes $\sin{x}$). Then by letting $t=\sin{x}$ and by using that $\cos^2{x} = 1 - \sin^2{x}$, we get an integral from a rational function of $t$.