Skip to content

Commit 91a32cf

Browse files
committed
support for pipe | notation in formula (unstable implementation), code update
1 parent 9c6dc1b commit 91a32cf

File tree

7 files changed

+43
-12
lines changed

7 files changed

+43
-12
lines changed

fdaPDE/src/formula.h

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,42 @@
2020
namespace fdapde {
2121

2222
class Formula {
23+
// available tokens
24+
struct efx_token {
25+
private:
26+
std::string cov_;
27+
std::string efx_;
28+
public:
29+
efx_token(const std::string& cov, const std::string& efx) : cov_(cov), efx_(efx) { }
30+
// observers
31+
const std::string& cov() const { return cov_; }
32+
const std::string& efx() const { return efx_; }
33+
friend std::ostream& operator<<(std::ostream& os, const efx_token& m) {
34+
os << "(" << m.cov_ << ", " << m.efx_ << ")";
35+
return os;
36+
}
37+
};
38+
2339
std::string lhs_;
24-
std::vector<std::string> rhs_;
40+
std::vector<std::string> covs_;
41+
std::vector<efx_token> efxs_;
2542

2643
void throw_parse_error_(const std::string& msg) {
2744
throw std::runtime_error(std::string("Formula parse error: ") + msg);
2845
}
46+
void analyze_token_(std::string token) {
47+
size_t pos = token.find('|');
48+
if (pos != std::string::npos) {
49+
std::string cov_token, efx_token;
50+
cov_token = token.substr(0, pos);
51+
token.erase(0, pos + 1);
52+
efx_token = token;
53+
efxs_.emplace_back(cov_token, efx_token);
54+
} else {
55+
covs_.push_back(token);
56+
}
57+
return;
58+
}
2959
public:
3060
Formula() noexcept = default;
3161
Formula(const std::string& formula) {
@@ -37,20 +67,21 @@ class Formula {
3767
// rhs parsing logic
3868
std::string rhs = formula.substr(tilde + 1, formula.size() - tilde - 1);
3969
std::erase(rhs, ' ');
40-
if (rhs.empty()) { throw_parse_error_("no rhs found."); }
70+
if (rhs.empty()) { throw_parse_error_("no rhs found."); }
4171
size_t pos = rhs.find('+');
4272
std::string token;
4373
while (pos != std::string::npos) {
4474
token = rhs.substr(0, pos);
45-
rhs_.push_back(token);
75+
analyze_token_(token);
4676
rhs.erase(0, pos + 1);
47-
pos = rhs.find('+');
77+
pos = rhs.find('+');
4878
}
49-
rhs_.push_back(rhs);
79+
analyze_token_(rhs);
5080
}
5181
// observers
5282
const std::string& lhs() const { return lhs_; }
53-
const std::vector<std::string>& rhs() const { return rhs_; }
83+
const std::vector<std::string>& covs() const { return covs_; }
84+
const std::vector<efx_token>& efxs() const { return efxs_; }
5485
};
5586

5687
} // namespace fdapde

fdaPDE/src/models/gsr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class GSRPDE {
6363
Formula formula_(formula);
6464
n_obs_ = gf[0].rows();
6565
n_covs_ = 0;
66-
for (const std::string& token : formula_.rhs()) {
66+
for (const std::string& token : formula_.covs()) {
6767
if (gf.contains(token)) { n_covs_++; }
6868
}
6969
solver_.analyze_data(formula, gf, W);

fdaPDE/src/models/qsr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ template <typename VariationalSolver> class QSRPDE {
4848
Formula formula_(formula);
4949
n_obs_ = gf[0].rows();
5050
n_covs_ = 0;
51-
for (const std::string& token : formula_.rhs()) {
51+
for (const std::string& token : formula_.covs()) {
5252
if (gf.contains(token)) { n_covs_++; }
5353
}
5454
solver_.analyze_data(formula, gf, W);

fdaPDE/src/models/sr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SRPDE {
4646
Formula formula_(formula);
4747
n_obs_ = gf[0].rows();
4848
n_covs_ = 0;
49-
for (const std::string& token : formula_.rhs()) {
49+
for (const std::string& token : formula_.covs()) {
5050
if (gf.contains(token)) { n_covs_++; }
5151
}
5252
solver_.analyze_data(formula, gf, W);

fdaPDE/src/solvers/fe_ls_elliptic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ struct fe_ls_elliptic {
201201
// parse formula, extract response vector and design matrix
202202
Formula formula_(formula);
203203
std::vector<std::string> covs;
204-
for (const std::string& token : formula_.rhs()) {
204+
for (const std::string& token : formula_.covs()) {
205205
if (gf.contains(token)) { covs.push_back(token); }
206206
}
207207
bool require_woodbury_realloc = std::cmp_not_equal(n_covs_, covs.size());

fdaPDE/src/solvers/fe_ls_parabolic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class fe_ls_parabolic_mono {
278278
// parse formula, extract response vector and design matrix
279279
Formula formula_(formula);
280280
std::vector<std::string> covs;
281-
for (const std::string& token : formula_.rhs()) {
281+
for (const std::string& token : formula_.covs()) {
282282
if (gf.contains(token)) { covs.push_back(token); }
283283
}
284284
bool require_woodbury_realloc = std::cmp_not_equal(n_covs_, covs.size());

fdaPDE/src/solvers/fe_ls_separable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class fe_ls_separable_mono {
278278
// parse formula, extract response vector and design matrix
279279
Formula formula_(formula);
280280
std::vector<std::string> covs;
281-
for (const std::string& token : formula_.rhs()) {
281+
for (const std::string& token : formula_.covs()) {
282282
if (gf.contains(token)) { covs.push_back(token); }
283283
}
284284
bool require_woodbury_realloc = std::cmp_not_equal(n_covs_, covs.size());

0 commit comments

Comments
 (0)