2024-05-18 18:01:41 +02:00
|
|
|
|
export {
|
2024-12-24 12:51:36 +01:00
|
|
|
|
import "int.lt";
|
|
|
|
|
import "euclidean.lt";
|
|
|
|
|
|
2024-05-18 18:01:41 +02:00
|
|
|
|
/* Implementation of Rational Numbers
|
|
|
|
|
*/
|
2024-12-24 12:51:36 +01:00
|
|
|
|
let ratio-sign = λ{
|
|
|
|
|
p: machine.Int64 ~ machine.Word;
|
|
|
|
|
q: machine.Int64 ~ machine.Word;
|
|
|
|
|
}
|
|
|
|
|
↦ bool-xor (int-sign p) (int-sign q);
|
2024-05-18 18:01:41 +02:00
|
|
|
|
|
|
|
|
|
let ratio-scale = λ{
|
2024-12-24 12:51:36 +01:00
|
|
|
|
{
|
|
|
|
|
p: machine.Int64~machine.Word;
|
|
|
|
|
q: machine.Int64~machine.Word;
|
|
|
|
|
} : ℚ ~ <Ratio machine.Int64~machine.Word>;
|
|
|
|
|
n : machine.Int64 ~machine.Word;
|
2024-05-18 18:01:41 +02:00
|
|
|
|
} ↦ {
|
|
|
|
|
i* q n;
|
|
|
|
|
i* p n;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let ratio-normalize = λ{
|
2024-12-24 12:51:36 +01:00
|
|
|
|
p: machine.Int64 ~ machine.Word;
|
|
|
|
|
q: machine.Int64 ~ machine.Word;
|
|
|
|
|
} : ℚ ~ <Ratio machine.Int64 ~ machine.Word>
|
2024-05-18 18:01:41 +02:00
|
|
|
|
↦ {
|
|
|
|
|
let s = gcd p q;
|
|
|
|
|
i/ q s;
|
|
|
|
|
i/ p s;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let ratio-add = λ{
|
2024-12-24 12:51:36 +01:00
|
|
|
|
{
|
|
|
|
|
ap:machine.Int64~machine.Word;
|
|
|
|
|
aq:machine.Int64~machine.Word;
|
|
|
|
|
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
|
|
|
|
{
|
|
|
|
|
bp:machine.Int64~machine.Word;
|
|
|
|
|
bq:machine.Int64~machine.Word;
|
|
|
|
|
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
2024-05-18 18:01:41 +02:00
|
|
|
|
} ↦ {
|
|
|
|
|
let l = lcm aq bq;
|
2024-09-30 11:27:35 +02:00
|
|
|
|
let a = i/ l aq;
|
|
|
|
|
let b = i/ l bq;
|
2024-05-18 18:01:41 +02:00
|
|
|
|
|
2024-09-30 11:27:35 +02:00
|
|
|
|
i* aq a;
|
|
|
|
|
i+ (i* ap a) (i* bp b);
|
2024-05-18 18:01:41 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let ratio-mul = λ{
|
2024-12-24 12:51:36 +01:00
|
|
|
|
{
|
|
|
|
|
ap:machine.Int64~machine.Word;
|
|
|
|
|
aq:machine.Int64~machine.Word;
|
|
|
|
|
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
|
|
|
|
{
|
|
|
|
|
bp:machine.Int64~machine.Word;
|
|
|
|
|
bq:machine.Int64~machine.Word;
|
|
|
|
|
}: ℚ ~ <Ratio machine.Int64 ~ machine.Word> ;
|
2024-05-18 18:01:41 +02:00
|
|
|
|
} ↦ ratio-normalize (i* ap bp) (i* aq bq);
|
|
|
|
|
|
2024-12-24 12:51:36 +01:00
|
|
|
|
/*
|
2024-05-18 18:01:41 +02:00
|
|
|
|
let fmt-ratio = λ{ p:ℤ; q:ℤ; }: ℚ~<Ratio ℤ> ↦ {
|
|
|
|
|
fmt-int q;':';fmt-int p;
|
|
|
|
|
};
|
2024-12-24 12:51:36 +01:00
|
|
|
|
*/
|
2024-05-18 18:01:41 +02:00
|
|
|
|
}
|
|
|
|
|
|