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$.