Skip to content

Commit 120f099

Browse files
committed
Restructured organization a bit
1 parent 02e9270 commit 120f099

File tree

11 files changed

+78
-52
lines changed

11 files changed

+78
-52
lines changed

content/number-theory/ModularArithmetic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ struct Mod {
2828
Mod r = *this ^ (e / 2); r = r * r;
2929
return e&1 ? *this * r : r;
3030
}
31-
explicit operator ll(){ return x; }
31+
explicit operator ll() const { return x; }
3232
};

content/numerical/PolynomialBase.h renamed to content/numerical/PolyBase.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212

1313
typedef Mod num;
1414
typedef vector<num> poly;
15-
vector<Mod> conv(vector<Mod> a, vector<Mod> b) {
16-
auto res = convMod<mod>(vl(all(a)), vl(all(b)));
17-
// auto res = conv(vl(all(a)), vl(all(b)));
18-
return vector<Mod>(all(res));
19-
}
2015
poly &operator+=(poly &a, const poly &b) {
2116
a.resize(max(sz(a), sz(b)));
2217
rep(i, 0, sz(b)) a[i] = a[i] + b[i];
@@ -35,7 +30,9 @@ poly &operator*=(poly &a, const poly &b) {
3530
res[i + j] = (res[i + j] + a[i] * b[j]);
3631
return (a = res);
3732
}
38-
return a = conv(a, b);
33+
auto res = convMod<mod>(vl(all(a)), vl(all(b)));
34+
// auto res = conv(vl(all(a)), vl(all(b)));
35+
return (a = poly(all(res)));
3936
}
4037
poly operator*(poly a, const num b) {
4138
poly c = a;
@@ -45,7 +42,7 @@ poly operator*(poly a, const num b) {
4542
#define OP(o, oe) \
4643
poly operator o(poly a, poly b) { \
4744
poly c = a; \
48-
return c oe b; \
45+
return c o##= b; \
4946
}
5047
OP(*, *=) OP(+, +=) OP(-, -=);
5148
poly modK(poly a, int k) { return {a.begin(), a.begin() + min(k, sz(a))}; }

content/numerical/PolynomialEval.h renamed to content/numerical/PolyEvaluate.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/**
2+
* Author: chilli, Andrew He, Adamant
3+
* Date: 2019-04-27
4+
* Description: Multi-point evaluation. Evaluates a given polynomial A at $A(x_0), ... A(x_n)$.
5+
* Time: O(n \log^2 n)
6+
*/
17
#pragma once
28

3-
#include "PolynomialMod.h"
9+
#include "PolyMod.h"
410

511
vector<num> eval(const poly &a, const vector<num> &x) {
612
int n = sz(x);
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
/**
2-
* Author: Simon Lindholm
3-
* Date: 2017-05-10
4-
* License: CC0
5-
* Source: Wikipedia
6-
* Description: Given $n$ points (x[i], y[i]), computes an n-1-degree polynomial $p$ that
7-
* passes through them: $p(x) = a[0]*x^0 + ... + a[n-1]*x^{n-1}$.
8-
* For numerical precision, pick $x[k] = c*\cos(k/(n-1)*\pi), k=0 \dots n-1$.
9-
* Time: O(n^2)
2+
* Author: chilli, Andrew He, Adamant
3+
* Date: 2019-04-27
4+
* Description: A FFT based Polynomial class.
105
*/
116
#pragma once
127

13-
typedef vector<double> vd;
14-
vd interpolate(vd x, vd y, int n) {
15-
vd res(n), temp(n);
16-
rep(k,0,n-1) rep(i,k+1,n)
17-
y[i] = (y[i] - y[k]) / (x[i] - x[k]);
18-
double last = 0; temp[0] = 1;
19-
rep(k,0,n) rep(i,0,n) {
20-
res[i] += y[k] * temp[i];
21-
swap(last, temp[i]);
22-
temp[i] -= last * x[k];
23-
}
24-
return res;
25-
}
8+
#include "PolyMod.h"
9+
#include "PolyPow.h"
10+
#include "PolyEvaluate.h"
11+
12+
poly interp(vector<num> x, vector<num> y) {
13+
int n=sz(x);
14+
vector<poly> up(n*2);
15+
rep(i,0,n) up[i+n] = poly({num(0)-x[i], num(1)});
16+
for(int i=n-1; i>0;i--) up[i] = up[2*i]*up[2*i+1];
17+
vector<num> a = eval(deriv(up[1]), x);
18+
vector<poly> down(2*n);
19+
rep(i,0,n) down[i+n] = poly({y[i]*(num(1)/a[i])});
20+
for(int i=n-1;i>0;i--) down[i] = down[i*2] * up[i*2+1] + down[i*2+1] * up[i*2];
21+
return down[1];
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Author: Simon Lindholm
3+
* Date: 2017-05-10
4+
* License: CC0
5+
* Source: Wikipedia
6+
* Description: Given $n$ points (x[i], y[i]), computes an n-1-degree polynomial $p$ that
7+
* passes through them: $p(x) = a[0]*x^0 + ... + a[n-1]*x^{n-1}$.
8+
* For numerical precision, pick $x[k] = c*\cos(k/(n-1)*\pi), k=0 \dots n-1$.
9+
* Time: O(n^2)
10+
*/
11+
#pragma once
12+
13+
typedef vector<double> vd;
14+
vd interpolate(vd x, vd y, int n) {
15+
vd res(n), temp(n);
16+
rep(k,0,n-1) rep(i,k+1,n)
17+
y[i] = (y[i] - y[k]) / (x[i] - x[k]);
18+
double last = 0; temp[0] = 1;
19+
rep(k,0,n) rep(i,0,n) {
20+
res[i] += y[k] * temp[i];
21+
swap(last, temp[i]);
22+
temp[i] -= last * x[k];
23+
}
24+
return res;
25+
}

content/numerical/PolynomialMod.h renamed to content/numerical/PolyMod.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
/**
2+
* Author: chilli, Andrew He, Adamant
3+
* Date: 2019-04-27
4+
* Description: A FFT based Polynomial class.
5+
*/
16
#pragma once
27

3-
#include "PolynomialBase.h"
8+
#include "PolyBase.h"
49

510
poly &operator/=(poly &a, poly b) {
611
if (sz(a) < sz(b))

content/numerical/PolynomialPow.h renamed to content/numerical/PolyPow.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
/**
2+
* Author: chilli, Andrew He, Adamant
3+
* Date: 2019-04-27
4+
* Description: A FFT based Polynomial class.
5+
*/
16
#pragma once
27

3-
#include "PolynomialBase.h"
8+
#include "PolyBase.h"
49
poly deriv(poly a) {
510
if (a.empty())
611
return {};

content/numerical/PolynomialInterpolate.h

Lines changed: 0 additions & 17 deletions
This file was deleted.

content/numerical/chapter.tex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
\chapter{Numerical}
22

33
\kactlimport{GoldenSectionSearch.h}
4-
\kactlimport{PolynomialBase.h}
4+
\kactlimport{PolyBase.h}
5+
\kactlimport{PolyMod.h}
6+
\kactlimport{PolyPow.h}
7+
\kactlimport{PolyInterpolate.h}
8+
\kactlimport{PolyEvaluate.h}
59
\kactlimport{PolyRoots.h}
610
\kactlimport{BerlekampMassey.h}
711
\kactlimport{LinearRecurrence.h}

fuzz-tests/numerical/Polynomial.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,17 @@ struct Mod {
412412
Mod r = *this ^ (e / 2); r = r * r;
413413
return e&1 ? *this * r : r;
414414
}
415-
explicit operator ll() { return x; }
415+
explicit operator ll() const { return x; }
416416
};
417417

418418
typedef Mod num;
419419
typedef vector<num> poly;
420420

421-
#include "../../content/numerical/FFTPolynomial.h"
421+
#include "../../content/numerical/PolyBase.h"
422+
#include "../../content/numerical/PolyMod.h"
423+
#include "../../content/numerical/PolyPow.h"
424+
#include "../../content/numerical/PolyEvaluate.h"
425+
#include "../../content/numerical/PolyInterpolate.h"
422426
} // namespace mine
423427

424428
pair<mine::poly, MIT::poly> genVec(int sz) {

0 commit comments

Comments
 (0)