summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/asymptote/rational.asy
blob: 0fa4ee36afa69b40f1fc5a640ddb69afd37ba4aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Asymptote module implementing rational arithmetic.

int gcd(int m, int n)
{
  if(m < n) {
    int temp=m;
    m=n;
    n=temp;
  }
  while(n != 0) {
    int r=m % n;
    m=n;
    n=r;
  }       
  return m;
}

int lcm(int m, int n)
{
  return m#gcd(m,n)*n;
}

struct rational {
  int p=0,q=1;
  void reduce() {
    int d=gcd(p,q);
    if(abs(d) > 1) {
      p #= d;
      q #= d;
    }
    if(q <= 0) {
      if(q == 0) abort("Division by zero");
      p=-p;
      q=-q;
    }
  }
  void operator init(int p=0, int q=1, bool reduce=true) {
    this.p=p;
    this.q=q;
    if(reduce) reduce();
  }
}

rational operator cast(int p) {
  return rational(p,false);
}

rational[] operator cast(int[] a) {
  return sequence(new rational(int i) {return a[i];},a.length);
}

rational[][] operator cast(int[][] a) {
  return sequence(new rational[](int i) {return a[i];},a.length);
}

real operator ecast(rational r) {
  return r.p/r.q;
}

rational operator -(rational r)
{
  return rational(-r.p,r.q,false);
}

rational operator +(rational r, rational s)
{
  return rational(r.p*s.q+s.p*r.q,r.q*s.q);
}

rational operator -(rational r, rational s)
{
  return rational(r.p*s.q-s.p*r.q,r.q*s.q);
}

rational operator *(rational r, rational s)
{
  return rational(r.p*s.p,r.q*s.q);
}

rational operator /(rational r, rational s)
{
  return rational(r.p*s.q,r.q*s.p);
}

bool operator ==(rational r, rational s)
{
  return r.p == s.p && r.q == s.q;
}

bool operator <(rational r, rational s)
{
  return r.p*s.q-s.p*r.q < 0;
}

bool operator >(rational r, rational s)
{
  return r.p*s.q-s.p*r.q > 0;
}

bool operator <=(rational r, rational s)
{
  return r.p*s.q-s.p*r.q <= 0;
}

bool operator >=(rational r, rational s)
{
  return r.p*s.q-s.p*r.q >= 0;
}

bool[] operator ==(rational[] r, rational s)
{
  return sequence(new bool(int i) {return r[i] == s;},r.length);
}

bool operator ==(rational[] r, rational[] s)
{
  if(r.length != s.length)
    abort(" operation attempted on arrays of different lengths: "+
          string(r.length)+" != "+string(s.length));
  return all(sequence(new bool(int i) {return r[i] == s[i];},r.length));
}

bool operator ==(rational[][] r, rational[][] s)
{
  if(r.length != s.length)
    abort(" operation attempted on arrays of different lengths: "+
          string(r.length)+" != "+string(s.length));
  return all(sequence(new bool(int i) {return r[i] == s[i];},r.length));
}

rational min(rational a, rational b)
{
  return a <= b ? a : b;
}

rational max(rational a, rational b)
{
  return a >= b ? a : b;
}


void write(string s="", rational r, suffix suffix=endl) {
  if(r.q == 1)
    write(s+string(r.p),suffix);
  else
    write(s+string(r.p)+"/"+string(r.q),suffix);
}

void write(string s="", rational[] a, suffix suffix=endl) {
  if(s != "")
    write(s);
  for(int i=0; i < a.length; ++i) {
    write(i,none);
    write(':\t',a[i]);
  }
  write(suffix);
}

void write(string s="", rational[][] a, suffix suffix=endl) {
  if(s != "")
    write(s);
  for(int i=0; i < a.length; ++i) {
    rational[] ai=a[i];
    for(int j=0; j < ai.length; ++j) {
      write(ai[j],tab);
    }
    write();
  }
  write(suffix);
}

bool rectangular(rational[][] m)
{
  int n=m.length;
  if(n > 0) {
    int m0=m[0].length;
    for(int i=1; i < n; ++i)
      if(m[i].length != m0) return false;
  }
  return true;
}

rational sum(rational[] a)
{
  rational sum;
  for(rational r:a)
    sum += r;
  return sum;
}

rational max(rational[] a)
{
  rational M=a[0];
  for(rational r:a)
    M=max(M,r);
  return M;
}

rational abs(rational r)
{
  return rational(abs(r.p),r.q,false);
}


/*
rational r=rational(1,3)+rational(1,4);
write(r == rational(1,12));
write(r);
real x=r;
write(x);

rational r=3;
write(r);

write(gcd(-8,12));
write(rational(-36,-14));

int[][] a=new int[][] {{1,2},{3,4}};
rational[][] r=a;
write(r);

*/