Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28495 views
License: GPL3
ubuntu2004
1
/* Copyright (C) 2017 The PARI group.
2
3
This file is part of the PARI/GP package.
4
5
PARI/GP is free software; you can redistribute it and/or modify it under the
6
terms of the GNU General Public License as published by the Free Software
7
Foundation; either version 2 of the License, or (at your option) any later
8
version. It is distributed in the hope that it will be useful, but WITHOUT
9
ANY WARRANTY WHATSOEVER.
10
11
Check the License for details. You should have received a copy of it, along
12
with the package; see the file 'COPYING'. If not, write to the Free Software
13
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14
15
#include "pari.h"
16
#include "paripriv.h"
17
18
void
19
forksubset_init(forsubset_t *T, long n, long k)
20
{
21
T->all = 0;
22
T->first = 1;
23
T->n = n;
24
T->k = k;
25
T->v = identity_perm(k);
26
}
27
28
void
29
forallsubset_init(forsubset_t *T, long n)
30
{
31
T->all = 1;
32
T->first = 1;
33
T->n = n;
34
T->k = 0;
35
T->v = vecsmalltrunc_init(n + 1);
36
}
37
38
static GEN
39
forksubset_next(forsubset_t *T)
40
{
41
GEN v = T->v;
42
long i, n = T->n, k = T->k;
43
44
if (T->first) { T->first = 0; return (k >= 0 && k <= n) ? v: NULL; }
45
if (k <= 0 || k >= n) return NULL;
46
47
if (v[k] < n) { v[k]++; return v; }
48
for (i = k - 1; i >= 1 && v[i+1] == v[i] + 1; i--);
49
if (i == 0) return NULL;
50
51
v[i]++;
52
for (; i < k; i++) v[i+1] = v[i] + 1;
53
return v;
54
}
55
static GEN
56
forallsubset_next(forsubset_t *T)
57
{
58
long i;
59
60
if (forksubset_next(T)) return T->v;
61
else if (T->k < T->n)
62
{
63
(T->k)++;
64
setlg(T->v, T->k+1);
65
for (i = 1; i <= T->k; i++) (T->v)[i] = i;
66
return T->v;
67
}
68
return NULL;
69
}
70
GEN
71
forsubset_next(forsubset_t *T)
72
{ return T->all? forallsubset_next(T): forksubset_next(T); }
73
void
74
forsubset_init(forsubset_t *T, GEN nk)
75
{
76
switch(typ(nk))
77
{
78
case t_INT: forallsubset_init(T, itos(nk)); return;
79
case t_VEC:
80
if (lg(nk) == 3)
81
{
82
GEN n = gel(nk,1), k = gel(nk,2);
83
if (typ(n) == t_INT && typ(k) == t_INT)
84
return forksubset_init(T, itos(n),itos(k));
85
}
86
default:
87
pari_err_TYPE("forsubset", nk);
88
}
89
}
90
void
91
forsubset0(GEN nk, GEN code)
92
{
93
pari_sp av = avma;
94
forsubset_t T;
95
void *E = (void*)code;
96
GEN v;
97
push_lex(gen_0, code);
98
forsubset_init(&T, nk);
99
while ((v = forsubset_next(&T)))
100
if (gp_evalvoid(E, v)) break;
101
pop_lex(1); set_avma(av);
102
}
103
104