3 * Interface to several small and furry utilities needed within GiNaC but not
4 * of any interest to the user of the library. */
7 * GiNaC Copyright (C) 1999-2017 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "assertion.h"
30 #include <cstdint> // for uintptr_t
35 /** Exception class thrown by functions to signal unimplemented functionality
36 * so the expression may just be .hold() */
39 // some compilers (e.g. cygwin) define a macro log2, causing confusion
44 unsigned log2(unsigned n);
46 /** Rotate bits of unsigned value by one bit to the left.
47 * This can be necessary if the user wants to define its own hashes. */
48 inline unsigned rotate_left(unsigned n)
50 return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
53 /** Compare two pointers (just to establish some sort of canonical order).
54 * @return -1, 0, or 1 */
56 inline int compare_pointers(const T * a, const T * b)
58 // '<' is not defined for pointers that don't point to the same array,
60 if (std::less<const T *>()(a, b))
62 else if (std::less<const T *>()(b, a))
67 /** Truncated multiplication with golden ratio, for computing hash values. */
68 inline unsigned golden_ratio_hash(uintptr_t n)
70 return n * UINT64_C(0x4f1bbcdd);
73 /* Compute the sign of a permutation of a container, with and without an
74 explicitly supplied comparison function. If the sign returned is 1 or -1,
75 the container is sorted after the operation. */
77 int permutation_sign(It first, It last)
89 It i = last, other = last;
98 } else if (!(*other < *i))
110 i = first; other = first;
119 } else if (!(*i < *other))
129 } while (first != last);
134 template <class It, class Cmp, class Swap>
135 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
146 It i = last, other = last;
148 bool swapped = false;
150 if (comp(*i, *other)) {
155 } else if (!comp(*other, *i))
167 i = first; other = first;
171 if (comp(*other, *i)) {
176 } else if (!comp(*i, *other))
186 } while (first != last);
191 /* Implementation of shaker sort, only compares adjacent elements. */
192 template <class It, class Cmp, class Swap>
193 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
203 It i = last, other = last;
205 bool swapped = false;
207 if (comp(*i, *other)) {
222 i = first; other = first;
226 if (comp(*other, *i)) {
239 } while (first != last);
242 /* In-place cyclic permutation of a container (no copying, only swapping). */
243 template <class It, class Swap>
244 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
246 unsigned num = last - first;
248 if (first == new_first || num < 2)
251 unsigned num1 = new_first - first, num2 = last - new_first;
253 It a = first, b = new_first;
264 It a = new_first, b = last;
268 } while (a != first);
275 /** Base class for generating all bounded combinatorial partitions of an integer
276 * n with exactly m parts in non-decreasing order.
278 class basic_partition_generator {
280 // Partitions n into m parts, not including zero parts.
281 // (Cf. OEIS sequence A008284; implementation adapted from Jörg Arndt's
285 // partition: x[1] + x[2] + ... + x[m] = n and sentinel x[0] == 0
286 std::vector<unsigned> x;
288 unsigned m; // 0<m<=n
289 mpartition2(unsigned n_, unsigned m_)
290 : x(m_+1), n(n_), m(m_)
292 for (unsigned k=1; k<m; ++k)
296 bool next_partition()
298 unsigned u = x[m]; // last element
307 return false; // current is last
308 unsigned f = x[k] + 1;
319 basic_partition_generator(unsigned n_, unsigned m_)
324 /** Generate all bounded combinatorial partitions of an integer n with exactly
325 * m parts (including zero parts) in non-decreasing order.
327 class partition_with_zero_parts_generator : public basic_partition_generator {
329 unsigned m; // number of parts 0<m
330 mutable std::vector<unsigned> partition; // current partition
331 mutable bool current_updated; // whether partition vector has been updated
333 partition_with_zero_parts_generator(unsigned n_, unsigned m_)
334 : basic_partition_generator(n_, 1), m(m_), partition(m_), current_updated(false)
336 // returns current partition in non-decreasing order, padded with zeros
337 const std::vector<unsigned>& get() const
339 if (!current_updated) {
340 for (unsigned i = 0; i < m - mpgen.m; ++i)
341 partition[i] = 0; // pad with zeros
343 for (unsigned i = m - mpgen.m; i < m; ++i)
344 partition[i] = mpgen.x[i - m + mpgen.m + 1];
350 current_updated = false;
351 if (!mpgen.next_partition()) {
352 if (mpgen.m == m || mpgen.m == mpgen.n)
353 return false; // current is last
354 // increment number of parts
355 mpgen = mpartition2(mpgen.n, mpgen.m + 1);
361 /** Generate all bounded combinatorial partitions of an integer n with exactly
362 * m parts (not including zero parts) in non-decreasing order.
364 class partition_generator : public basic_partition_generator {
366 mutable std::vector<unsigned> partition; // current partition
367 mutable bool current_updated; // whether partition vector has been updated
369 partition_generator(unsigned n_, unsigned m_)
370 : basic_partition_generator(n_, m_), partition(m_), current_updated(false)
372 // returns current partition in non-decreasing order, padded with zeros
373 const std::vector<unsigned>& get() const
375 if (!current_updated) {
376 for (unsigned i = 0; i < mpgen.m; ++i)
377 partition[i] = mpgen.x[i + 1];
383 current_updated = false;
384 return mpgen.next_partition();
388 /** Generate all compositions of a partition of an integer n, starting with the
389 * compositions which has non-decreasing order.
391 class composition_generator {
393 // Generates all distinct permutations of a multiset.
394 // (Based on Aaron Williams' algorithm 1 from "Loopless Generation of
395 // Multiset Permutations using a Constant Number of Variables by Prefix
396 // Shifts." <http://webhome.csc.uvic.ca/~haron/CoolMulti.pdf>)
398 // element of singly linked list
402 element(unsigned val, element* n)
403 : value(val), next(n) {}
405 { // recurses down to the end of the singly linked list
409 element *head, *i, *after_i;
410 // NB: Partition must be sorted in non-decreasing order.
411 explicit coolmulti(const std::vector<unsigned>& partition)
412 : head(nullptr), i(nullptr), after_i(nullptr)
414 for (unsigned n = 0; n < partition.size(); ++n) {
415 head = new element(partition[n], head);
422 { // deletes singly linked list
425 void next_permutation()
428 if (after_i->next != nullptr && i->value >= after_i->next->value)
432 element *k = before_k->next;
433 before_k->next = k->next;
435 if (k->value < head->value)
440 bool finished() const
442 return after_i->next == nullptr && after_i->value >= head->value;
445 bool atend; // needed for simplifying iteration over permutations
446 bool trivial; // likewise, true if all elements are equal
447 mutable std::vector<unsigned> composition; // current compositions
448 mutable bool current_updated; // whether composition vector has been updated
450 explicit composition_generator(const std::vector<unsigned>& partition)
451 : cmgen(partition), atend(false), trivial(true), composition(partition.size()), current_updated(false)
453 for (unsigned i=1; i<partition.size(); ++i)
454 trivial = trivial && (partition[0] == partition[i]);
456 const std::vector<unsigned>& get() const
458 if (!current_updated) {
459 coolmulti::element* it = cmgen.head;
461 while (it != nullptr) {
462 composition[i] = it->value;
466 current_updated = true;
472 // This ugly contortion is needed because the original coolmulti
473 // algorithm requires code duplication of the payload procedure,
474 // one before the loop and one inside it.
475 if (trivial || atend)
477 cmgen.next_permutation();
478 current_updated = false;
479 atend = cmgen.finished();
484 /** Compute the multinomial coefficient n!/(p1!*p2!*...*pk!) where
485 * n = p1+p2+...+pk, i.e. p is a partition of n.
488 multinomial_coefficient(const std::vector<unsigned> & p);
491 // Collection of `construct on first use' wrappers for safely avoiding
492 // internal object replication without running into the `static
493 // initialization order fiasco'. This chest of numbers helps speed up
494 // the library but should not be used outside it since it is
495 // potentially confusing.
499 extern const numeric *_num_120_p;
500 extern const ex _ex_120;
501 extern const numeric *_num_60_p;
502 extern const ex _ex_60;
503 extern const numeric *_num_48_p;
504 extern const ex _ex_48;
505 extern const numeric *_num_30_p;
506 extern const ex _ex_30;
507 extern const numeric *_num_25_p;
508 extern const ex _ex_25;
509 extern const numeric *_num_24_p;
510 extern const ex _ex_24;
511 extern const numeric *_num_20_p;
512 extern const ex _ex_20;
513 extern const numeric *_num_18_p;
514 extern const ex _ex_18;
515 extern const numeric *_num_15_p;
516 extern const ex _ex_15;
517 extern const numeric *_num_12_p;
518 extern const ex _ex_12;
519 extern const numeric *_num_11_p;
520 extern const ex _ex_11;
521 extern const numeric *_num_10_p;
522 extern const ex _ex_10;
523 extern const numeric *_num_9_p;
524 extern const ex _ex_9;
525 extern const numeric *_num_8_p;
526 extern const ex _ex_8;
527 extern const numeric *_num_7_p;
528 extern const ex _ex_7;
529 extern const numeric *_num_6_p;
530 extern const ex _ex_6;
531 extern const numeric *_num_5_p;
532 extern const ex _ex_5;
533 extern const numeric *_num_4_p;
534 extern const ex _ex_4;
535 extern const numeric *_num_3_p;
536 extern const ex _ex_3;
537 extern const numeric *_num_2_p;
538 extern const ex _ex_2;
539 extern const numeric *_num_1_p;
540 extern const ex _ex_1;
541 extern const numeric *_num_1_2_p;
542 extern const ex _ex_1_2;
543 extern const numeric *_num_1_3_p;
544 extern const ex _ex_1_3;
545 extern const numeric *_num_1_4_p;
546 extern const ex _ex_1_4;
547 extern const numeric *_num0_p;
548 extern const basic *_num0_bp;
549 extern const ex _ex0;
550 extern const numeric *_num1_4_p;
551 extern const ex _ex1_4;
552 extern const numeric *_num1_3_p;
553 extern const ex _ex1_3;
554 extern const numeric *_num1_2_p;
555 extern const ex _ex1_2;
556 extern const numeric *_num1_p;
557 extern const ex _ex1;
558 extern const numeric *_num2_p;
559 extern const ex _ex2;
560 extern const numeric *_num3_p;
561 extern const ex _ex3;
562 extern const numeric *_num4_p;
563 extern const ex _ex4;
564 extern const numeric *_num5_p;
565 extern const ex _ex5;
566 extern const numeric *_num6_p;
567 extern const ex _ex6;
568 extern const numeric *_num7_p;
569 extern const ex _ex7;
570 extern const numeric *_num8_p;
571 extern const ex _ex8;
572 extern const numeric *_num9_p;
573 extern const ex _ex9;
574 extern const numeric *_num10_p;
575 extern const ex _ex10;
576 extern const numeric *_num11_p;
577 extern const ex _ex11;
578 extern const numeric *_num12_p;
579 extern const ex _ex12;
580 extern const numeric *_num15_p;
581 extern const ex _ex15;
582 extern const numeric *_num18_p;
583 extern const ex _ex18;
584 extern const numeric *_num20_p;
585 extern const ex _ex20;
586 extern const numeric *_num24_p;
587 extern const ex _ex24;
588 extern const numeric *_num25_p;
589 extern const ex _ex25;
590 extern const numeric *_num30_p;
591 extern const ex _ex30;
592 extern const numeric *_num48_p;
593 extern const ex _ex48;
594 extern const numeric *_num60_p;
595 extern const ex _ex60;
596 extern const numeric *_num120_p;
597 extern const ex _ex120;
600 // Helper macros for class implementations (mostly useful for trivial classes)
602 #define DEFAULT_CTOR(classname) \
603 classname::classname() { setflag(status_flags::evaluated | status_flags::expanded); }
605 #define DEFAULT_COMPARE(classname) \
606 int classname::compare_same_type(const basic & other) const \
608 /* by default, the objects are always identical */ \
612 #define DEFAULT_PRINT(classname, text) \
613 void classname::do_print(const print_context & c, unsigned level) const \
618 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
619 DEFAULT_PRINT(classname, text) \
620 void classname::do_print_latex(const print_latex & c, unsigned level) const \
627 #endif // ndef GINAC_UTILS_H