Solving linear system modulo prime^n when matrix is univertable

5 次查看(过去 30 天)

Hello

Please adivise is there a way to use built-in tools to solve a linear system modulo p^m? Matrix rank is below max so LU and / are generating error. System is known to have multipe solutions. The task is to either find general solution or a particular one.
Thanks

回答(1 个)

John D'Errico
John D'Errico 2025-3-1
编辑:John D'Errico 2025-3-1
You can't use LU anyway, since it is not designed to work on that problem. Nor can you use slash or backslash, which again, do not understand arithmetic mod anything. So you can't use the built-in tools to do this.
I may have posted a code I called rrefgf on the FEX. The idea is that the basic linear algebra tools CAN be written to work in the multiplicative group of integers modulo some number. And when I say can be written, one would need to write them, as I did with rrefgf.
It all works well enough when the modulus is prime, because then everything has a multiplicative inverse. (Well, not zero, but that is irrelevant.) But when you are working mod p^m, things get a little harder.
I also posted a tool called modinv, It, or something equivalent, is necessary when working on problems like this. (modinv was easily written using gcd in MATLAB.) For example, if I do this:
modinv(2:24,25)
ans =
Columns 1 through 16
13 17 19 NaN 21 18 22 14 NaN 16 23 2 9 NaN 11 3
Columns 17 through 23
7 4 NaN 6 8 12 24
As you can see, 5,10,15,20 all lack a multiplicative inverse, when treated in the mutiplicative group of integers mod 25. And that makes it all much more difficult.
However, nothing is impossible. For example, we might do this:
P = 25;
Aprob = magic(5);
X0 = randi(24,[5,1])
X0 = 5×1
2 20 17 9 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
b = mod(Aprob*X0,P)
b = 5×1
8 8 18 8 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So we know a solution does exist, because I created b from a known solution. (Aprob should be rank deficient in that field however.) Now solve the problem, at least for a particular solution. GA can probably handle it, as long as I give it some leeway.
obj = @(X) sum((mod(Aprob*X(:),P) - b).^2);
intcon = 1:5;
LB = zeros(5,1);
UB = (P-1)*ones(5,1);
opts = optimoptions('ga',maxgenerations = 100000,populationsize = 200);
[Xga,fval,exitflag] = ga(obj,5,[],[],[],[],LB,UB,[],intcon,opts)
ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance.
Xga = 1×5
6 4 21 3 21
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = 0
exitflag = 1
A different solution, but equally good, since this problem lacks a unique solution. I could also have used intlinprog on the same problem, and not have worried so much about convergence, as might be an issue with GA.
f = [zeros(5,1);ones(5,1)]; % introucing slack variables to handle mod.
Ai = [Aprob,25*eye(5)];
LB = [zeros(5,1);-inf*ones(5,1)];
UB = [24*ones(5,1);inf*ones(5,1)];
intcon = 1:10;
[Xi,fval,exitflag] = intlinprog(f,intcon,[],[],Ai,b,LB,UB)
Running HiGHS 1.7.0: Copyright (c) 2024 HiGHS under MIT licence terms Coefficient ranges: Matrix [1e+00, 2e+01] Cost [1e+00, 1e+00] Bound [2e+01, 2e+01] RHS [8e+00, 2e+01] Presolving model 5 rows, 10 cols, 30 nonzeros 0s 5 rows, 10 cols, 30 nonzeros 0s Objective function is integral with scale 1 Solving MIP model with: 5 rows 10 cols (0 binary, 10 integer, 0 implied int., 0 continuous) 30 nonzeros Nodes | B&B Tree | Objective Bounds | Dynamic Constraints | Work Proc. InQueue | Leaves Expl. | BestBound BestSol Gap | Cuts InLp Confl. | LpIters Time 0 0 0 0.00% -309 inf inf 0 0 0 0 0.0s 0 0 0 0.00% -307.9909091 inf inf 0 0 3 11 0.0s T 1091 162 391 65.83% -294.9171179 -219 34.67% 316 10 2148 2001 0.2s T 1704 178 606 76.39% -289.04 -245 17.98% 167 15 3116 2600 0.2s T 3577 176 1293 90.56% -284.9189189 -258 10.43% 50 7 6008 4570 0.4s T 4184 91 1536 96.15% -284.3040936 -271 4.91% 58 7 6842 5268 0.5s Solving report Status Optimal Primal bound -271 Dual bound -271 Gap 0% (tolerance: 0.01%) Solution status feasible -271 (objective) 0 (bound viol.) 0 (int. viol.) 0 (row viol.) Timing 0.49 (total) 0.00 (presolve) 0.00 (postsolve) Nodes 4413 LP iterations 5671 (total) 236 (strong br.) 223 (separation) 355 (heuristics) Optimal solution found. Intlinprog stopped because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 1e-06. The intcon variables are integer within tolerance, options.ConstraintTolerance = 1e-06.
Xi = 10×1
21 19 21 23 21 -53 -55 -55 -55 -53
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = -271
exitflag = 1
How well did it do?
Xi = round(Xi(1:5)); % The round makes sure Xi is all truly integer
mod(Aprob*Xi - b,P)
ans = 5×1
0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
As you can see, intlinprog also works, although the solution is again different. There is no expectation it will find the same solution as anything else on a rank deficient problem. Do you really want the fully general solution? That can probably be done too, possibly using my rrefgf code, or something like it.
  2 个评论
Vadim
Vadim 2025-3-1
John, first of all, thank you for such a great response. Much appreciate it. I certainly need to spend a couple quiet hours and walk through it. I like your approach with minimization and linearprog. Not sure about GA within Matlab, no experice, but I expect it to mean geneteic algorythm.
General solution is not the final goal, you are correct. The goal is to find the simpliest polynomial that represents multiple-variable k-logic function over extended field. This basically means less components that represent higher power in such polynomial. Or less non-zero "higher" row index values. Nullspace would do it but in my understanding neither Matlab nor Maple can do Gauss elimination with respect to a lack of multiplicative inverse as you point out above.
One of the routes is to sort matrix in the way that bruteforcing begins with most simpliest monomes but some of these functions may have no polynomial at all. So with dimension curse it can work really bad for thouse.
The other way is to wite elimination code but that's another story I suppose)
John D'Errico
John D'Errico 2025-3-1
编辑:John D'Errico 2025-3-2
The simplest solution is the one I showed that used intlinprog. You can play with the vector f to get other solutions. The important trick is in my use of slack variables, which changes the modular problem into one where the mods go away. (That one is worth remembering on this class of problem.)
You could also take a read through rrefgf, but it might be a little more to wade through than you want. It just uses basic elimination, but in the given group of integers.
I can and probably should write a nullspace code for this. Sigh. Easier for me to do than others. Some days that list of round-tuits gets long. ;-)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by