7 static int lex_cmp(vec_ZZ
& a
, vec_ZZ
& b
)
9 assert(a
.length() == b
.length());
11 for (int j
= 0; j
< a
.length(); ++j
)
13 return a
[j
] < b
[j
] ? -1 : 1;
17 static int lex_cmp(mat_ZZ
& a
, mat_ZZ
& b
)
19 assert(a
.NumCols() == b
.NumCols());
20 int alen
= a
.NumRows();
21 int blen
= b
.NumRows();
22 int len
= alen
< blen
? alen
: blen
;
24 for (int i
= 0; i
< len
; ++i
) {
25 int s
= lex_cmp(a
[i
], b
[i
]);
32 void gen_fun::add(ZZ
& cn
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den
)
37 short_rat
* r
= new short_rat
;
38 r
->n
.coeff
.SetDims(1, 2);
39 r
->n
.coeff
[0][0] = cn
;
40 r
->n
.coeff
[0][1] = cd
;
41 r
->n
.power
.SetDims(1, num
.length());
45 for (int i
= 0; i
< r
->d
.power
.NumRows(); ++i
) {
47 for (j
= 0; j
< r
->d
.power
.NumCols(); ++j
)
48 if (r
->d
.power
[i
][j
] != 0)
50 if (r
->d
.power
[i
][j
] < 0) {
51 r
->d
.power
[i
] = -r
->d
.power
[i
];
52 r
->n
.coeff
[0][0] = -r
->n
.coeff
[0][0];
53 r
->n
.power
[0] += r
->d
.power
[i
];
57 for (int i
= 0; i
< term
.size(); ++i
)
58 if (lex_cmp(term
[i
]->d
.power
, r
->d
.power
) == 0) {
59 int len
= term
[i
]->n
.coeff
.NumRows();
60 int dim
= term
[i
]->n
.power
.NumCols();
61 term
[i
]->n
.coeff
.SetDims(len
+1, 2);
62 term
[i
]->n
.power
.SetDims(len
+1, dim
);
63 term
[i
]->n
.coeff
[len
] = r
->n
.coeff
[0];
64 term
[i
]->n
.power
[len
] = r
->n
.power
[0];
72 static void print_power(vec_ZZ
& c
, vec_ZZ
& p
,
73 unsigned int nparam
, char **param_name
)
77 for (int i
= 0; i
< p
.length(); ++i
) {
81 if (c
[0] == -1 && c
[1] == 1)
83 else if (c
[0] != 1 || c
[1] != 1) {
86 cout
<< " / " << c
[1];
93 cout
<< param_name
[i
];
99 cout
<< "^(" << p
[i
] << ")";
106 cout
<< " / " << c
[1];
110 void gen_fun::print(unsigned int nparam
, char **param_name
)
116 for (int i
= 0; i
< term
.size(); ++i
) {
120 for (int j
= 0; j
< term
[i
]->n
.coeff
.NumRows(); ++j
) {
121 if (j
!= 0 && term
[i
]->n
.coeff
[j
][0] > 0)
123 print_power(term
[i
]->n
.coeff
[j
], term
[i
]->n
.power
[j
],
127 for (int j
= 0; j
< term
[i
]->d
.power
.NumRows(); ++j
) {
131 print_power(mone
, term
[i
]->d
.power
[j
],