Macaulay2 Engine
Loading...
Searching...
No Matches
pfaff.cpp
Go to the documentation of this file.
1// Copyright 1996 Michael E. Stillman.
2
3#include "pfaff.hpp"
4#include "comb.hpp"
5#include "interrupted.hpp"
6
8 : R(M0->get_ring()), M(M0), p(p0), done(false), row_set(nullptr)
9{
10 pfaffs = MatrixConstructor(R->make_FreeModule(1), 0);
11 if (p == 0)
12 {
13 pfaffs.append(R->make_vec(0, R->one()));
14 done = true;
15 return;
16 }
17 if (p % 2 != 0 || p < 0 || (p > M->n_cols()) || (p > M->n_rows()))
18 {
19 done = true;
20 return;
21 }
22 row_set = newarray_atomic(size_t, p);
23 for (int i = 0; i < p; i++) row_set[i] = i;
24}
25
28// Compute one more pfafferminant of size p.
29// increments I and/or J and updates 'pfaffs', 'table'.
30{
31 if (done) return COMP_DONE;
32
34 if (!R->is_zero(r))
35 pfaffs.append(R->make_vec(0, r));
36 else
37 R->remove(r);
38
39 if (Subsets::increment(M->n_cols(), p, row_set)) return COMP_COMPUTING;
40
41 // Otherwise, we are at the end.
42 done = true;
43 return COMP_DONE;
44}
45
47{
48 for (;;)
49 {
50 int result = step();
51 if (result == COMP_DONE) return COMP_DONE;
52 if (--nsteps == 0) return COMP_DONE_STEPS;
54 }
55}
56
61
63// Compute the pfaffian of the (skew symmetric)
64// minor with rows and columns r[0]..r[p2-1].
65// assumption: p2 is an even number.
66{
67 if (p2 == 2) return M->elem(static_cast<int>(r[0]), static_cast<int>(r[1]));
68 ring_elem result = R->from_long(0);
69
70 bool negate = true;
71 for (int i = p2 - 2; i >= 0; i--)
72 {
73 std::swap(r[i], r[p2 - 2]);
74 negate = !negate;
75 ring_elem g =
76 M->elem(static_cast<int>(r[p2 - 2]), static_cast<int>(r[p2 - 1]));
77 if (R->is_zero(g))
78 {
79 R->remove(g);
80 continue;
81 }
82 ring_elem h = calc_pfaff(r, p2 - 2);
83 ring_elem gh = R->mult(g, h);
84 R->remove(g);
85 R->remove(h);
86 if (negate)
87 R->subtract_to(result, gh);
88 else
89 R->add_to(result, gh);
90 }
91
92 // pulling out the columns has disordered r. Fix it.
93
94 size_t temp = r[p2 - 2];
95 for (size_t i = p2 - 2; i > 0; i--) r[i] = r[i - 1];
96 r[0] = temp;
97
98 return result;
99}
100
102{
103 if (get_ring()->get_precision() > 0)
104 {
105 ERROR("pfaffian computations over RR or CC not yet implemented");
106 return nullptr;
107 }
108 PfaffianComputation d {this, p};
109 d.calc();
110 return d.pfaffians();
111}
112
114{
115 PfaffianComputation d {this, n_cols()};
116
117 return d.calc_pfaff();
118}
119
120// Local Variables:
121// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
122// indent-tabs-mode: nil
123// End:
const Ring * get_ring() const
Definition matrix.hpp:134
int n_cols() const
Definition matrix.hpp:147
Matrix(const FreeModule *rows, const FreeModule *cols, const_monomial degree_shift, VECTOR(vec) &entries)
Definition matrix.cpp:32
Matrix * pfaffians(int p) const
Definition pfaff.cpp:101
ring_elem pfaffian() const
Definition pfaff.cpp:113
Mutable builder used to assemble an immutable Matrix one column (or one term) at a time.
const Ring * R
Definition pfaff.hpp:51
ring_elem calc_pfaff(void)
Definition pfaff.cpp:57
ring_elem calc_pfaff(size_t *r, int p)
Definition pfaff.cpp:62
Matrix * pfaffians()
Definition pfaff.hpp:75
MatrixConstructor pfaffs
Definition pfaff.hpp:53
size_t * row_set
Definition pfaff.hpp:58
PfaffianComputation(const Matrix *M, int p)
Definition pfaff.cpp:7
const Matrix * M
Definition pfaff.hpp:52
int calc(int nsteps=-1)
Definition pfaff.cpp:46
const Ring * get_ring() const
Definition pfaff.hpp:76
Computation of pfaffians of a skew symmetric matrix.
Definition pfaff.hpp:50
static bool increment(size_t n, Subset &s)
Definition comb.cpp:124
Subsets — combinatorial-number-system encoding of p-subsets of {0,...,n-1}.
@ COMP_DONE_STEPS
Definition computation.h:69
@ COMP_DONE
Definition computation.h:60
@ COMP_COMPUTING
Definition computation.h:71
@ COMP_INTERRUPTED
Definition computation.h:57
#define Matrix
Definition factory.cpp:14
int p
bool system_interrupted()
system_interrupted() — thread-safe polling predicate for Ctrl+C handling.
void freemem(void *s)
Definition m2-mem.cpp:103
const int ERROR
Definition m2-mem.cpp:55
VALGRIND_MAKE_MEM_DEFINED & result(result)
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
Definition mpreal.h:3244
#define newarray_atomic(T, len)
Definition newdelete.hpp:91
PfaffianComputation — Pfaffians of skew-symmetric matrices via row expansion.