Post Reply 
[Prime CAS] ichinrem negative results
03-07-2014, 08:25 AM
Post: #1
[Prime CAS] ichinrem negative results
The CAS ichinrem function (Integer Chinese Remainder) returns often negative results.
For example:
ichinrem([4 7],[1 9]) = [-17 63]
ichinrem([4 11],[1 13]) = [-51 143]
ichinrem([-17 63],[-51 143]) = [235 9009]
and:
ichinrem([4 7],[1 9],[4 11],[1 13]) = [-8774 9009]

While both results [235 9009] and [-8774 9009] are valid and equivalent, I would prefer to have always the same result a modulo b with a in the interval [0,b[
Find all posts by this user
Quote this message in a reply
03-07-2014, 09:31 AM (This post was last modified: 03-07-2014 09:32 AM by Dominik Holenstein.)
Post: #2
RE: [Prime CAS] ichinrem negative results
An option could be the chinrem() function:

Code:

Input: chinrem([4 11],[1 13]) 
Result:[0 143]

Input: chinrem([4 7],[1 9])
Result: [0 63]

Input: chinrem([-17 63], [-51 143])
Result: [0 9009]

You can find more information on chinrem() and ichinrem() here:
http://www-fourier.ujf-grenoble.fr/~pari...tml#htoc48

Dominik
Find all posts by this user
Quote this message in a reply
03-07-2014, 11:54 PM
Post: #3
RE: [Prime CAS] ichinrem negative results
(03-07-2014 08:25 AM)Didier Lachieze Wrote:  The CAS ichinrem function (Integer Chinese Remainder) returns often negative results. ... While both results [235 9009] and [-8774 9009] are valid and equivalent, I would prefer to have always the same result a modulo b with a in the interval [0,b[

The same problem drives me crazy on the HP 50g. Like the Prime, its CAS modulo functions (not MOD itself) sometimes return negative results. I think your only hope is to create a user-defined function or program which uses the old MOD function to rectify the result.

Here's a tiny program that takes the matrix input for ichinrem and returns the smallest positive result:
Code:
EXPORT chin(M0)
BEGIN
LOCAL m;
m:=ichinrem(M0);
RETURN m(1) MOD m(2);
END;
Example:
ichinrem([[12,23],[123,234]]) --> [-5025, 5382]
chin([[12,23],[123,234]]) --> 357

If you want the modulus also, change "RETURN m(1) MOD m(2)" in the program to "RETURN [m(1) MOD m(2),m(2)]". Then the program would return [357, 5382] for the above example.

I really wish there were a more elegant way to do this, but that's how I've handled it on the 50g for many years.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
03-09-2014, 08:49 AM
Post: #4
RE: [Prime CAS] ichinrem negative results
Joe, thanks, your solution works fine. I was not aware ichinrem would work on a matrix.

However it would be better if the "RETURN [m(1) MOD m(2),m(2)]" was done by the ichinrem function itself.
Find all posts by this user
Quote this message in a reply
03-09-2014, 04:34 PM
Post: #5
RE: [Prime CAS] ichinrem negative results
(03-09-2014 08:49 AM)Didier Lachieze Wrote:  I was not aware ichinrem would work on a matrix.

It even works if the matrix contains more than two rows. Example: What number is congruent to 1 (mod 2), 3 (mod 4), and 1 (mod 6)?

ichinrem([[1,2], [3,4], [1,6]]) --> [7,12]
Answer: 7 (mod 12).

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
03-09-2014, 05:11 PM
Post: #6
RE: [Prime CAS] ichinrem negative results
(03-09-2014 08:49 AM)Didier Lachieze Wrote:  However it would be better if the "RETURN [m(1) MOD m(2),m(2)]" was done by the ichinrem function itself.

Same goes for the %% operator. It also usually spits out negative values. While correct I also would find it nicer when it would output the positive values.
Find all posts by this user
Quote this message in a reply
05-28-2014, 06:39 AM
Post: #7
RE: [Prime CAS] ichinrem negative results
(03-09-2014 08:49 AM)Didier Lachieze Wrote:  Joe, thanks, your solution works fine. I was not aware ichinrem would work on a matrix.

However it would be better if the "RETURN [m(1) MOD m(2),m(2)]" was done by the ichinrem function itself.

Smile Great, this is fixed in the new firmware 2014.0331 (even if I don't find it in the list of Resolved issues and changes in CAS).
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)