Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Solve a linear system with multiple moduli
ResourceFunction["LinearSolveMod"][mat,rhs,moduli] solves the linear system mat.x=rhs for the unknown vector x, with each equation matj . x-rhsj taken modulo modulij. | |
ResourceFunction["LinearSolveMod"][mat,rhs,moduli,True] solves the linear system and returns a list containing the solution vector and a set of vectors that span the null space. |
Solve the system (a+b=0mod2,b+c=0mod2,a+b+c=2mod3):
In[1]:= | ![]() |
Out[1]= | ![]() |
Check the result:
In[2]:= | ![]() |
Out[2]= | ![]() |
Solve the linear system:
In[3]:= | ![]() |
Out[3]= | ![]() |
LinearSolveMod can take Gaussian integers for input:
In[4]:= | ![]() |
Out[4]= | ![]() |
Obtain a solution and a spanning set of null vectors for a modular system:
In[5]:= | ![]() |
Out[5]= | ![]() |
Solve a simple system of the form x=2mod5,x=1mod7,x=4mod11:
In[6]:= | ![]() |
Out[6]= | ![]() |
This is equivalent to using ChineseRemainder on the values and moduli lists:
In[7]:= | ![]() |
Out[7]= | ![]() |
Solve a certain system modulo 29:
In[8]:= | ![]() |
Out[8]= | ![]() |
The modulus is a prime so LinearSolve can also solve it:
In[9]:= | ![]() |
Out[9]= | ![]() |
The resource function SmallIntegerSolve can find the solution and also the respective multiples of the modulus that provide it:
In[10]:= | ![]() |
Out[10]= | ![]() |
Check this:
In[11]:= | ![]() |
Out[11]= | ![]() |
Solve a system modulo 32:
In[12]:= | ![]() |
Out[12]= | ![]() |
We learn (noisily) that it cannot be solved using LinearSolve:
In[13]:= | ![]() |
Out[13]= | ![]() |
Since there is a common modulus, this can be handled using Solve or SolveValues:
In[14]:= | ![]() |
Out[14]= | ![]() |
Using LinearSolveMod, we can recover the second solution by obtaining the null vectors and adding a certain integer combination thereof to the solution:
In[15]:= | ![]() |
Out[15]= | ![]() |
Use the nontrivial null vector (the first one) to obtain the second solution given by SolveValues:
In[16]:= | ![]() |
Out[16]= | ![]() |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License