Sidef/Types/Number/Quadratic.pm untested

filter: Statementstmt 0.0% 0 / 299 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 87 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 0.0% 0 / 62 Podpod 0.0% 0 / 57 totaltotal 0.0% 0 / 605 CC 26 SCAR 21.3
CC
26
Cov
0%
new
42.0
_pretty_stringify
20.0
pow
20.0
CRAP 8.4
1 package Sidef::Types::Number::Quadratic;
2  
3 use utf8;
4 use 5.016;
5  
6 use parent qw(
7 Sidef::Types::Number::Number
8 );
9  
10 use overload
11 q{bool} => sub { (@_) = ($_[0]); goto &__boolify__ },
12 q{""} => sub { (@_) = ($_[0]); goto &__stringify__ },
13 q{0+} => \&to_n,
14 q{${}} => \&to_n;
15  
16 # Constructor: Quadratic(a, b, p, q) where t^2 = p + q*t
17 sub new {
18 my ($class, $A, $B, $p, $q) = @_;
19  
20 # Handle evaluation of polynomials
21 if (ref($_[0]) eq __PACKAGE__) {
22 return $_[0]->eval($A);
23 }
24  
25 # Ensure all arguments are proper Number objects
26 $A //= Sidef::Types::Number::Number::ZERO;
27 $B //= Sidef::Types::Number::Number::ZERO;
28 $p //= Sidef::Types::Number::Number::ONE;
29 $q //= Sidef::Types::Number::Number::ZERO;
30  
31 $A = Sidef::Types::Number::Number->new($A) if !UNIVERSAL::isa($A, 'Sidef::Types::Number::Number');
32 $B = Sidef::Types::Number::Number->new($B) if !UNIVERSAL::isa($B, 'Sidef::Types::Number::Number');
33 $p = Sidef::Types::Number::Number->new($p) if !UNIVERSAL::isa($p, 'Sidef::Types::Number::Number');
34 $q = Sidef::Types::Number::Number->new($q) if !UNIVERSAL::isa($q, 'Sidef::Types::Number::Number');
35  
36 bless {a => $A, b => $B, p => $p, q => $q};
37 }
38  
39 *call = \&new;
40  
41 sub with_value {
42 my ($self, $value_1, $value_2) = @_;
43 __PACKAGE__->new($value_1, $value_2, $self->{p}, $self->{q});
44 }
45  
46 # Evaluate as a polynomial at $x (replace t by $x)
47 sub eval {
48 my ($self, $x) = @_;
49 __PACKAGE__->new($self->{a}->eval($x), $self->{b}->eval($x), $self->{p}->eval($x), $self->{q}->eval($x));
50 }
51  
52 # Lift coefficients (used by modular arithmetic)
53 sub lift {
54 my ($self) = @_;
55 __PACKAGE__->new($self->{a}->lift, $self->{b}->lift, $self->{p}->lift, $self->{q}->lift);
56 }
57  
58 # Accessors
59 sub a { $_[0]->{a} }
60 *re = \&a;
61 *real = \&a;
62  
63 sub b { $_[0]->{b} }
64 *im = \&b;
65 *imag = \&b;
66  
67 sub p { $_[0]->{p} }
68 sub q { $_[0]->{q} }
69  
70 *w = \&p;
71  
72 sub reals {
73 ($_[0]->{a}, $_[0]->{b});
74 }
75  
76 sub parts {
77 Sidef::Types::Array::Array->new($_[0]->{a}, $_[0]->{b}, $_[0]->{p}, $_[0]->{q});
78 }
79  
80 # Boolean context (true unless zero)
81 sub __boolify__ {
82 $_[0]->{a};
83 }
84  
85 # Numification returns the complex embedding (principal root)
86 sub to_n {
87 my ($self) = @_;
88  
89 state $two = Sidef::Types::Number::Number::TWO;
90 state $four = $two->add($two);
91  
92 my $D = $self->{q}->sqr->add($self->{p}->mul($four)); # discriminant q^2 + 4p
93 my $sqrt_D = $D->sqrt; # complex if D<0, real otherwise
94  
95 my $t = $self->{q}->add($sqrt_D)->div($two); # principal root (q + sqrt(D))/2
96 my $r = $self->{a}->add($self->{b}->mul($t));
97  
98 if (ref($r) ne 'Sidef::Types::Number::Number') {
99 return $r->to_n;
100 }
101  
102 return $r;
103 }
104  
105 *to_c = \&to_n;
106  
107 # --- Stringification ---
108  
109 sub __stringify__ {
110 my ($self) = @_;
111 sprintf("Quadratic(%s, %s, %s, %s)", $self->{a}->dump, $self->{b}->dump, $self->{p}->dump, $self->{q}->dump);
112 }
113  
114 sub _pretty_stringify {
115 my ($x) = @_;
116  
117 if (!$x->{q}->is_zero) {
118 return $x->__stringify__;
119 }
120  
121 my $a_str = $x->{a}->stringify;
122  
123 if ($x->{b}->is_zero) {
124 return $a_str;
125 }
126  
127 my $p_str = $x->{p}->stringify;
128 my $sign = $x->{b}->is_neg ? ' - ' : ' + ';
129 my $b_abs_str = $x->{b}->abs->stringify;
130  
131 Sidef::Types::String::String->new($a_str . $sign . $b_abs_str . '*sqrt(' . $p_str . ')');
132 }
133  
134 sub stringify {
135 my ($x) = @_;
136 Sidef::Types::String::String->new($x->_pretty_stringify);
137 }
138  
139 *pretty = \&stringify;
140  
141 sub to_s {
142 my ($x) = @_;
143 Sidef::Types::String::String->new($x->__stringify__);
144 }
145  
146 *dump = \&to_s;
147  
148 # --- Arithmetic operations (scalar- and ring-element-aware) ---
149  
150 sub add {
151 my ($x, $y) = @_;
152 if (ref($y) eq __PACKAGE__ and $x->{p}->eq($y->{p}) and $x->{q}->eq($y->{q})) {
153 return __PACKAGE__->new($x->{a}->add($y->{a}), $x->{b}->add($y->{b}), $x->{p}, $x->{q});
154 }
155  
156 # Scalar addition: treat other as a+0*t
157 return __PACKAGE__->new($x->{a}->add($y), $x->{b}, $x->{p}, $x->{q});
158 }
159  
160 sub sub {
161 my ($x, $y) = @_;
162 if (ref($y) eq __PACKAGE__ and $x->{p}->eq($y->{p}) and $x->{q}->eq($y->{q})) {
163 return __PACKAGE__->new($x->{a}->sub($y->{a}), $x->{b}->sub($y->{b}), $x->{p}, $x->{q});
164 }
165 return __PACKAGE__->new($x->{a}->sub($y), $x->{b}, $x->{p}, $x->{q});
166 }
167  
168 sub mul {
169 my ($x, $y) = @_;
170 if (ref($y) eq __PACKAGE__ and $x->{p}->eq($y->{p}) and $x->{q}->eq($y->{q})) {
171 my $A = $x->{a};
172 my $B = $x->{b};
173 my $C = $y->{a};
174 my $D = $y->{b};
175 my $BD = $B->mul($D);
176 my $new_a = $A->mul($C)->add($BD->mul($x->{p}));
177 my $new_b = $A->mul($D)->add($B->mul($C))->add($BD->mul($x->{q}));
178 return __PACKAGE__->new($new_a, $new_b, $x->{p}, $x->{q});
179 }
180  
181 # Scalar multiplication
182 return __PACKAGE__->new($x->{a}->mul($y), $x->{b}->mul($y), $x->{p}, $x->{q});
183 }
184  
185 sub div {
186 my ($x, $y) = @_;
187 if (ref($y) eq __PACKAGE__ and $x->{p}->eq($y->{p}) and $x->{q}->eq($y->{q})) {
188 my $norm = $y->norm;
189 my $num = $x->mul($y->conj);
190 return __PACKAGE__->new($num->{a}->div($norm), $num->{b}->div($norm), $x->{p}, $x->{q});
191 }
192  
193 # Scalar division
194 return __PACKAGE__->new($x->{a}->div($y), $x->{b}->div($y), $x->{p}, $x->{q});
195 }
196  
197 # --- Unary operations ---
198  
199 sub neg {
200 my ($x) = @_;
201 __PACKAGE__->new($x->{a}->neg, $x->{b}->neg, $x->{p}, $x->{q});
202 }
203  
204 sub sqr {
205 my ($x) = @_;
206 $x->mul($x);
207 }
208  
209 sub abs {
210 my ($x) = @_;
211 $x->norm->sqrt;
212 }
213  
214 # --- Increment / Decrement (on the real part) ---
215  
216 sub inc {
217 my ($x) = @_;
218 __PACKAGE__->new($x->{a}->inc, $x->{b}, $x->{p}, $x->{q});
219 }
220  
221 sub dec {
222 my ($x) = @_;
223 __PACKAGE__->new($x->{a}->dec, $x->{b}, $x->{p}, $x->{q});
224 }
225  
226 # --- Rounding and floating point ---
227  
228 sub float {
229 my ($x) = @_;
230 __PACKAGE__->new($x->{a}->float, $x->{b}->float, $x->{p}, $x->{q});
231 }
232  
233 sub floor {
234 my ($x) = @_;
235 __PACKAGE__->new($x->{a}->floor, $x->{b}->floor, $x->{p}, $x->{q});
236 }
237  
238 sub ceil {
239 my ($x) = @_;
240 __PACKAGE__->new($x->{a}->ceil, $x->{b}->ceil, $x->{p}, $x->{q});
241 }
242  
243 sub round {
244 my ($x, $r) = @_;
245 __PACKAGE__->new($x->{a}->round($r), $x->{b}->round($r), $x->{p}, $x->{q});
246 }
247  
248 # --- Euclidean division ---
249  
250 # Euclidean division with rounding to nearest (gives better behaviour for Euclidean algorithm)
251 sub divmod {
252 my ($self, $other) = @_;
253 my $q = $self->mul($other->inv)->round;
254 my $r = $self->sub($q->mul($other));
255 return ($q, $r);
256 }
257  
258 sub idiv {
259 my ($self, $other) = @_;
260 my ($q, undef) = $self->divmod($other);
261 return $q;
262 }
263  
264 sub mod {
265 my ($x, $y) = @_;
266  
267 if (ref($y) eq 'Sidef::Types::Number::Number') {
268 return __PACKAGE__->new($x->{a}->mod($y), $x->{b}->mod($y), $x->{p}, $x->{q});
269 }
270  
271 # Euclidean remainder
272 my (undef, $r) = $x->divmod($y);
273 return $r;
274 }
275  
276 # --- Shifts (multiplication/division by 2^n) ---
277  
278 sub shift_left {
279 my ($x, $n) = @_;
280 $x->mul(Sidef::Types::Number::Number::TWO->pow($n));
281 }
282 *lsft = \&shift_left;
283  
284 sub shift_right {
285 my ($x, $n) = @_;
286 $x->div(Sidef::Types::Number::Number::TWO->pow($n));
287 }
288 *rsft = \&shift_right;
289  
290 # --- Exponentiation ---
291  
292 sub pow {
293 my ($x, $n) = @_;
294 $n->is_int || return $x->to_n->pow($n); # non‑integer -> complex embedding
295  
296 my $negative_power = 0;
297 if ($n->is_neg) {
298 $n = $n->abs;
299 $negative_power = 1;
300 }
301  
302 my $c = __PACKAGE__->new(Sidef::Types::Number::Number::ONE, Sidef::Types::Number::Number::ZERO, $x->{p}, $x->{q});
303  
304 my $base = $x;
305 foreach my $bit (reverse split(//, $n->as_bin)) {
306 $c = $c->mul($base) if $bit;
307 $base = $base->sqr;
308 }
309  
310 $negative_power ? $c->inv : $c;
311 }
312  
313 sub powmod {
314 my ($x, $n, $m) = @_;
315 $x = $x->mod($m);
316 my $negative_power = 0;
317 if ($n->is_neg) {
318 $n = $n->abs;
319 $negative_power = 1;
320 }
321  
322 my $c = __PACKAGE__->new(Sidef::Types::Number::Number::ONE, Sidef::Types::Number::Number::ZERO, $x->{p}, $x->{q});
323  
324 my $base = $x;
325 foreach my $bit (reverse split(//, $n->as_bin)) {
326 $c = $c->mul($base)->mod($m) if $bit;
327 $base = $base->sqr->mod($m);
328 }
329  
330 $negative_power ? $c->invmod($m) : $c;
331 }
332  
333 # --- Algebraic properties ---
334  
335 sub conj {
336 my ($x) = @_;
337 __PACKAGE__->new($x->{a}->add($x->{b}->mul($x->{q})), $x->{b}->neg, $x->{p}, $x->{q});
338 }
339  
340 sub norm {
341 my ($x) = @_;
342 $x->{a}->sqr->add($x->{a}->mul($x->{b})->mul($x->{q}))->sub($x->{b}->sqr->mul($x->{p}));
343 }
344  
345 sub discriminant {
346 my ($self) = @_;
347 state $two = Sidef::Types::Number::Number::TWO;
348 state $four = $two->add($two);
349 $self->{q}->sqr->add($self->{p}->mul($four));
350 }
351  
352 sub roots {
353 my ($self) = @_;
354 state $two = Sidef::Types::Number::Number::TWO;
355 my $sqrt_D = $self->discriminant->sqrt;
356 my $t1 = $self->{q}->add($sqrt_D)->div($two);
357 my $t2 = $self->{q}->sub($sqrt_D)->div($two);
358 Sidef::Types::Array::Array->new([$t1, $t2]);
359 }
360  
361 sub is_real_embedding {
362 my ($self) = @_;
363 $self->discriminant->ge(Sidef::Types::Number::Number::ZERO);
364 }
365  
366 sub trace {
367 my ($x) = @_;
368 $x->{a}->mul(Sidef::Types::Number::Number::TWO)->add($x->{b}->mul($x->{q}));
369 }
370  
371 sub inv {
372 my ($x) = @_;
373 my $norm = $x->norm;
374 my $conj_a = $x->{a}->add($x->{b}->mul($x->{q}));
375 my $conj_b = $x->{b}->neg;
376 __PACKAGE__->new($conj_a->div($norm), $conj_b->div($norm), $x->{p}, $x->{q});
377 }
378  
379 sub invmod {
380 my ($x, $m) = @_;
381 $x = $x->mod($m);
382 my $norm = $x->norm->invmod($m);
383 my $conj_a = $x->{a}->add($x->{b}->mul($x->{q}));
384 my $conj_b = $x->{b}->neg;
385 __PACKAGE__->new($conj_a->mul($norm)->mod($m), $conj_b->mul($norm)->mod($m), $x->{p}, $x->{q});
386 }
387  
388 # --- Predicates ---
389  
390 sub is_zero {
391 my ($x) = @_;
392 $x->{a}->is_zero && $x->{b}->is_zero;
393 }
394  
395 sub is_one {
396 my ($x) = @_;
397 $x->{a}->is_one && $x->{b}->is_zero;
398 }
399  
400 sub is_mone {
401 my ($x) = @_;
402 $x->{a}->is_mone && $x->{b}->is_zero;
403 }
404  
405 sub is_int {
406 $_[0]->{b}->is_zero;
407 }
408  
409 sub is_unit {
410 my ($x) = @_;
411 $x->norm->abs->is_one;
412 }
413  
414 sub is_associate {
415 my ($x, $y) = @_;
416 return $x->is_zero ? $y->is_zero : !$y->is_zero && $x->mul($y->inv)->is_unit;
417 }
418  
419 sub is_coprime {
420 my ($x, $y) = @_;
421 $x->norm->gcd($y->norm)->is_one;
422 }
423  
424 sub is_prime {
425 my ($x) = @_;
426  
427 # sufficient condition: norm is a prime integer (not necessary though)
428 $x->norm->abs->is_prime;
429 }
430  
431 # --- GCD / LCM (Euclidean algorithm) ---
432  
433 sub gcd {
434 my ($self, $other) = @_;
435 my $u = $self;
436 my $v = $other;
437 while (!$v->is_zero) {
438 my (undef, $r) = $u->divmod($v);
439 $u = $v;
440 $v = $r;
441 }
442 return $u;
443 }
444  
445 sub lcm {
446 my ($self, $other) = @_;
447 if ($self->is_zero || $other->is_zero) {
448 return __PACKAGE__->new(Sidef::Types::Number::Number::ZERO, Sidef::Types::Number::Number::ZERO, $self->{p}, $self->{q});
449 }
450 $self->mul($other)->div($self->gcd($other));
451 }
452  
453 # --- Comparison (lexicographic: a, b, p, q) ---
454  
455 sub cmp {
456 my ($x, $y) = @_;
457 if (ref($y) eq __PACKAGE__) {
458 my $cmp = $x->{a}->cmp($y->{a}) // return undef;
459 $cmp and return $cmp;
460 $cmp = $x->{b}->cmp($y->{b}) // return undef;
461 $cmp and return $cmp;
462 $cmp = $x->{p}->cmp($y->{p}) // return undef;
463 $cmp and return $cmp;
464 return $x->{q}->cmp($y->{q});
465 }
466  
467 # Compare with a plain number: a vs. $y, then b vs. 0
468 my $cmp = $x->{a}->cmp($y) // return undef;
469 return $cmp if $cmp;
470 $x->{b}->cmp(Sidef::Types::Number::Number::ZERO);
471 }
472  
473 sub eq {
474 my ($x, $y) = @_;
475 if (ref($y) eq __PACKAGE__) {
476 $x->{a}->eq($y->{a}) && $x->{b}->eq($y->{b}) && $x->{p}->eq($y->{p}) && $x->{q}->eq($y->{q});
477 }
478 else {
479 $x->{a}->eq($y) && $x->{b}->is_zero;
480 }
481 }
482  
483 sub ne {
484 my ($x, $y) = @_;
485 !$x->eq($y);
486 }
487  
488 # --- Operator overloading (inspired by Quadratic) ---
489  
490 {
491 no strict 'refs';
492  
493 foreach my $method (qw(ge gt lt le)) {
494 *{__PACKAGE__ . '::' . $method} = sub {
495 my ($x, $y) = @_;
496 ($x->cmp($y) // return undef)->$method(Sidef::Types::Number::Number::ZERO);
497 };
498 }
499  
500 foreach my $method (qw(and xor or)) {
501 *{__PACKAGE__ . '::' . $method} = sub {
502 my ($x, $y) = @_;
503 if (ref($y) eq __PACKAGE__ and $x->{p}->eq($y->{p}) and $x->{q}->eq($y->{q})) {
504 return __PACKAGE__->new($x->{a}->$method($y->{a}), $x->{b}->$method($y->{b}), $x->{p}, $x->{q});
505 }
506  
507 # Treat $y as scalar with same ring
508 return __PACKAGE__->new($x->{a}->$method($y), $x->{b}, $x->{p}, $x->{q});
509 };
510 }
511  
512 # Map infix operators to methods
513 *{__PACKAGE__ . '::' . '/'} = \&div;
514 *{__PACKAGE__ . '::' . '÷'} = \&div;
515 *{__PACKAGE__ . '::' . '*'} = \&mul;
516 *{__PACKAGE__ . '::' . '%'} = \&mod;
517 *{__PACKAGE__ . '::' . '+'} = \&add;
518 *{__PACKAGE__ . '::' . '-'} = \&sub;
519 *{__PACKAGE__ . '::' . '**'} = \&pow;
520 *{__PACKAGE__ . '::' . '++'} = \&inc;
521 *{__PACKAGE__ . '::' . '--'} = \&dec;
522 *{__PACKAGE__ . '::' . '<'} = \&lt;
523 *{__PACKAGE__ . '::' . '>'} = \&gt;
524 *{__PACKAGE__ . '::' . '&'} = \&and;
525 *{__PACKAGE__ . '::' . '|'} = \&or;
526 *{__PACKAGE__ . '::' . '^'} = \&xor;
527 *{__PACKAGE__ . '::' . '<<'} = \&lsft;
528 *{__PACKAGE__ . '::' . '>>'} = \&rsft;
529 *{__PACKAGE__ . '::' . '<=>'} = \&cmp;
530 *{__PACKAGE__ . '::' . '<='} = \&le;
531 *{__PACKAGE__ . '::' . '≤'} = \&le;
532 *{__PACKAGE__ . '::' . '>='} = \&ge;
533 *{__PACKAGE__ . '::' . '≥'} = \&ge;
534 *{__PACKAGE__ . '::' . '=='} = \&eq;
535 *{__PACKAGE__ . '::' . '!='} = \&ne;
536 }
537  
538 1