Solving a linear system of equations with constraints

523 Views Asked by At

Q) I have a finite state space $S$ of size $n$ and $f:S\to \mathbb{R}$. $A,B\subset S$. $L$ is a $n\times n$ matrix such that all row sums = $0$. Also $f(A)=0$ and $f(B)=1$.

I am trying to find a $f$, a vector of size $n$, such that $f$ satisfies $(Lf)_x=0$ for $x\not\in S\setminus\{A\cup B\}$ where $(Lf)_x$ is the value corresponding to state $x$ in the vector $Lf$ with the aforementioned boundary conditions, i.e. $f(A)=0$ and $f(B)=1$, in Python. Any suggestions?

For example: let $S=\{0,1,2,3\}, f(0)=0, f(3)=1$ $$ L= \begin{bmatrix} -1 & 1 & 0 & 0 \\ 1 & -2 & 1 & 0 \\ 0 & 1 & -2 & 1 \\ 0 & 0 & 1 & -1 \\ \end{bmatrix} $$

and solving $(Lf)_x=0$ for states $x=1,2$, we can see that

$$ Lf= \begin{bmatrix} -1 & 1 & 0 & 0 \\ 1 & -2 & 1 & 0 \\ 0 & 1 & -2 & 1 \\ 0 & 0 & 1 & -1 \\ \end{bmatrix} \begin{bmatrix} 0 \\ x_1 \\ x_2 \\ 1 \\ \end{bmatrix} = \begin{bmatrix} . \\ 0 \\ 0 \\ . \\ \end{bmatrix} $$

i.e. $x_1=1/3, x_2=2/3$. That is $f(1)=1/3,f(2)=2/3$.

So I want to be able to do this for a big matrix $L$ in Python.

1

There are 1 best solutions below

0
On BEST ANSWER

I think the best way to program this is to pass the matrix $L$, the indices of the two dummy rows (in order), and the values in those two rows. The function would first delete the two dummy rows (rows $0$ and $3$ in the example.)

Then construct a vector $B$ of length $n-2$ as follows. Say the dummy indices are $a$ and $b$ and the associated values are $\alpha$ and $\beta$. Then the value of $B[r]$ is -\alpha*L[r,a]-\beta*L[r,b]. Finally, delete columns $a$ and $b$ from $L$ and solve $LX=B$.

Note that a simpler way to compute $B$ is

`B=-alpha*L[:,a]-\beta*L[:,b]`