-
-
Save DoctorAjayKumar/1738854edcfa90dd626fe454f3cef1f0 to your computer and use it in GitHub Desktop.
MFP 4 code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
v = dict() | |
v[54321] = np.matrix('1 0') | |
v[ 9876] = np.matrix('0 1') | |
v[ 4941] = 1*v[54321] - 5*v[9876] | |
v[ 4935] = 1*v[ 9876] - 1*v[4941] | |
v[ 6] = 1*v[ 4941] - 1*v[4935] | |
v[ 3] = 1*v[ 4935] - 822*v[ 6] | |
# print v[54321] | |
# print v[ 9876] | |
# print v[ 4941] | |
# print v[ 4935] | |
# print v[ 6] | |
# print v[ 3] | |
M = np.matrix('54321; 9876') | |
print v[54321] * M | |
print v[ 9876] * M | |
print v[ 4941] * M | |
print v[ 4935] * M | |
print v[ 6] * M | |
print v[ 3] * M | |
# print M * v[ 3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(egcd). | |
-export([egcd/2]). | |
-export([ixr/2]). | |
% egcd = extended greatest common divisor | |
egcd(A, B) when A >= B, A >= 0, B >= 0 -> | |
V_Acc_Init = #{A => row(1, 0), | |
B => row(0, 1)}, | |
egcd_plus(A, B, V_Acc_Init). | |
egcd_plus(GCD, 0, V_Acc_Final) -> | |
{egcd, V_Acc_Final, maps:get(GCD, V_Acc_Final)}; | |
egcd_plus(A, B, V_Acc) -> | |
% A = Q*B + R | |
% R = 1*A + -Q*B | |
Q = A div B, | |
R = A rem B, | |
V_A = maps:get(A, V_Acc), | |
V_B = maps:get(B, V_Acc), | |
V_R = ilc([{ 1, V_A}, | |
{-1*Q, V_B}]), | |
V_Acc_New = maps:put(R, V_R, V_Acc), | |
egcd_plus(B, R, V_Acc_New). | |
% row | |
row(A, B) -> | |
{row, A, B}. | |
% integer linear combination (integer-weighted sum) | |
ilc(Items) -> | |
RowAcc_Init = row(0, 0), | |
ilc(Items, RowAcc_Init). | |
ilc([], RowAcc_Final) -> | |
RowAcc_Final; | |
ilc([{Int, Row} | Rest], RowAcc) -> | |
RowAcc_New = rowplus(ixr(Int, Row), RowAcc), | |
ilc(Rest, RowAcc_New). | |
% ixr = integer times row | |
ixr(Int, {row, A, B}) -> | |
{row, Int*A, Int*B}. | |
% rowplus = add rows | |
rowplus({row, A, B}, {row, C, D}) -> | |
{row, A+C, B+D}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment