Re: [Scilab-users] Variable user equation input (2, 3 or more)
Hello, > De : De la part de Lester Anderson > Envoyé : samedi 30 avril 2022 07:15 > > My query, is it possible to make this more generalised and vary the > number of input equations and give the user an option to specify the number > of equations? I don't know this problem and its solution but as a first idea, I'd use matrices for the remainders and moduli, one line per equation (so n lines for n equations). So 1. Ask the number of equations n 2. Loop to ask the moduli and remainders, and fill in the lines of the matrices. Regards -- Christophe Dang Ngoc Chan Mechanical calculation engineer General This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Variable user equation input (2, 3 or more)
As a test, I increased the value n (iterations) and that forced it to converge on the value 150999. Sounds like I need a test of convergence. On Sun, 1 May 2022 at 08:37, Lester Anderson wrote: > Having an issue with the code: > > Tried: x ≡ 2 mod 11; 3 mod 12; 4 mod 13; 5 mod 17; 6 mod 19 (result should > be 150999 but get 442719) > Also checked x ≡ 4 mod 11; 3 mod 17 (result should be 37 but get 136) > However, 3 equations work fine! > > I have pinned down the problem to M_inv, where it is not looping through > all elements (m) but doing one less (eg 4 instead of 5). > > for i=1:length(m) > for j=1:n > if modulo(((M_ratio(i)*j)-1),m(i)) == 0 > M_inv(i)=j; > xn(i)=(a(i)*M_ratio(i)*M_inv(i)); > end > endend > > With the code snippet above, it should iterate 5 times for 5 input values of > a and m, it is not generating M_inv(5), or M_inv(2) (2 equations). > > I have verified what the results should be in Maxima CAS, > > [image: image.png] > > Any ideas what is going wrong? > > Thanks > > On Sun, 1 May 2022 at 05:32, Lester Anderson > wrote: > >> Many thanks Samuel >> >> On Sat, 30 Apr 2022 at 17:28, Samuel Gougeon wrote: >> >>> Le 30/04/2022 à 18:26, Samuel Gougeon a écrit : >>> >>> Le 30/04/2022 à 18:14, Lester Anderson a écrit : >>> >>> Sorted the code with multiple equations. Not too sure how to deal with >>> the messagebox list of equations. >>> Still have to do that manually - unless there is a way to automatically >>> build this? >>> >>> Can this multiple text string be done via a for loop to build a list ? >>> >>> Thanks >>> >>> >>> Please \n as trailer for the 2nd msprintf as well: >>> >>> [ msprintf("x ≡ %d mod %d\n", a(:), m(:)) >>>msprintf("The solution for (x): %d mod %d\n", x(:), M(:)) >>> 'Solution of 3 congruence equations'] >>> >>> >>> ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Variable user equation input (2, 3 or more)
Having an issue with the code: Tried: x ≡ 2 mod 11; 3 mod 12; 4 mod 13; 5 mod 17; 6 mod 19 (result should be 150999 but get 442719) Also checked x ≡ 4 mod 11; 3 mod 17 (result should be 37 but get 136) However, 3 equations work fine! I have pinned down the problem to M_inv, where it is not looping through all elements (m) but doing one less (eg 4 instead of 5). for i=1:length(m) for j=1:n if modulo(((M_ratio(i)*j)-1),m(i)) == 0 M_inv(i)=j; xn(i)=(a(i)*M_ratio(i)*M_inv(i)); end endend With the code snippet above, it should iterate 5 times for 5 input values of a and m, it is not generating M_inv(5), or M_inv(2) (2 equations). I have verified what the results should be in Maxima CAS, [image: image.png] Any ideas what is going wrong? Thanks On Sun, 1 May 2022 at 05:32, Lester Anderson wrote: > Many thanks Samuel > > On Sat, 30 Apr 2022 at 17:28, Samuel Gougeon wrote: > >> Le 30/04/2022 à 18:26, Samuel Gougeon a écrit : >> >> Le 30/04/2022 à 18:14, Lester Anderson a écrit : >> >> Sorted the code with multiple equations. Not too sure how to deal with >> the messagebox list of equations. >> Still have to do that manually - unless there is a way to automatically >> build this? >> >> Can this multiple text string be done via a for loop to build a list ? >> >> Thanks >> >> >> Please \n as trailer for the 2nd msprintf as well: >> >> [ msprintf("x ≡ %d mod %d\n", a(:), m(:)) >>msprintf("The solution for (x): %d mod %d\n", x(:), M(:)) >> 'Solution of 3 congruence equations'] >> >> >> ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Variable user equation input (2, 3 or more)
Sorted the code with multiple equations. Not too sure how to deal with the messagebox list of equations. Still have to do that manually - unless there is a way to automatically build this? Can this multiple text string be done via a for loop to build a list ? Thanks ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Variable user equation input (2, 3 or more)
Cleaned up the code using Loops clear // Chinese Remainder Theorem (CRT) for 3 congruence equations// e.g. x ≡ 3 mod 5; x ≡ 1 mod 7; x ≡ 6 mod 8// result ≡ 78 mod 280 m=evstr(x_dialog(['Moduli values: '],'1'));a=evstr(x_dialog(['Remainder values: '],'1'));m1=m(1); m2=m(2); m3=m(3);a1=a(1); a2=a(2); a3=a(3);n=evstr(x_dialog(['Number of iterations: '],'1')); M=prod(m); for i=1:length(m) M_ratio(i) = M/m(i)end for i=1:length(m) for j=1:n if modulo(((M_ratio(i)*j)-1),m(i)) == 0 M_inv(i)=j; xn(i)=(a(i)*M_ratio(i)*M_inv(i)); end endend x = modulo(sum(xn),M) // Console output://mprintf('\nThe solution for (x): %d mod %d',x, M) messagebox(['x ≡ '+string(a1)'+' mod '+string(m1)' ... 'x ≡ '+string(a2)'+' mod '+string(m2)' ... 'x ≡ '+string(a3)'+' mod '+string(m3)' ... 'The solution for (x): '+string(x)+' mod '+string(M)'], ... 'Solution of 3 congruence equations') On Sat, 30 Apr 2022 at 06:14, Lester Anderson wrote: > Hello all, > > I have a simple code which computes applies the Chinese Remainder theorem > (for a single variable) given three input congruence equations to solve for > x; not very elegant but it works: > > My query, is it possible to make this more generalised and vary the number > of input equations and give the user an option to specify the number of > equations? > For example, one may have 2, 4 or more equations. > > Any pointers would be helpful. > > Thanks > Lester > > clear > // Chinese Remainder Theorem (CRT) for 3 congruence equations// e.g. x ≡ 3 > mod 5; x ≡ 1 mod 7; x ≡ 6 mod 8// result ≡ 78 mod 280 > m=evstr(x_dialog(['Moduli values: '],'1'));r=evstr(x_dialog(['Remainder > values: '],'1'));m1=m(1); m2=m(2); m3=m(3);r1=r(1); r2=r(2); > r3=r(3);n=evstr(x_dialog(['Number of iterations: '],'1')); > M=m1*m2*m3;M1=M/m1; M2=M/m2; M3=M/m3; > for i=1:n > if modulo(((M1*i)-1),m1) == 0 then > M1_inv=i; > end > > if modulo(((M2*i)-1),m2) == 0 then > M2_inv=i; > end > > if modulo(((M3*i)-1),m3) == 0 then > M3_inv=i; > endend > x1=(r1*M1*M1_inv);x2=(r2*M2*M2_inv);x3=(r3*M3*M3_inv); > x = modulo((x1 + x2 + x3), M); > messagebox(['x ≡ '+string(r1)'+' mod '+string(m1)' ... > 'x ≡ '+string(r2)'+' mod '+string(m2)' ... > 'x ≡ '+string(r3)'+' mod '+string(m3)' ... > 'The solution for (x): '+string(x)+' mod '+string(M)'], > ... > 'Solution of 3 congruence equations') > > > > > > ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users