Commit 7251832d authored by Alvarez, Gonzalo's avatar Alvarez, Gonzalo
Browse files

CanonicalExpression: supports complex scalars

parent cac25b8f
......@@ -77,13 +77,20 @@ private:
QuasiCanonicalType& quasiCanonical,
AuxiliaryType& aux) const
{
bool isCaScalar = isCanonicalScalar(termStr);
bool isCaScalar = QuasiCanonicalType::isRealScalar(termStr);
if (isCaScalar) {
const ComplexOrRealType f = PsimagLite::atof(termStr);
factor *= f;
return;
}
bool isPureCmplx = QuasiCanonicalType::isPureComplex(termStr);
if (isPureCmplx) {
const ComplexOrRealType f = QuasiCanonicalType::pureComplex(termStr);
factor *= f;
return;
}
const int ind = quasiCanonical.scalarIndex(termStr);
if (ind >= 0) {
const ComplexOrRealType f = quasiCanonical.scalarFromIndex(ind);
......@@ -103,54 +110,6 @@ private:
prev *= term;
}
bool isCanonicalScalar(String termStr) const
{
if (termStr.length() == 0)
err("CanonicalExpression: term must no be empty\n");
char c = termStr[0];
bool isDigit = (c >= '0' && c <= '9');
return (c == '.' || c == '-' || c == '+' || isDigit);
}
// Deal with complex here, FIXME
// ComplexOrRealType findFactor(String termStr,
// bool prevHadParens,
// RealType* dummy) const
// {
// SizeType l = termStr.length();
// if (l == 0)
// err("CanonicalExpression: scalar must no be empty\n");
// char c = termStr[0];
// bool isDigit = (c >= '0' && c <= '9');
// if (c == '.') termStr = "0" + termStr;
// if (isDigit || c == '.') return atof(termStr.c_str());
// if (c == '-') {
// if (!prevHadParens)
// err("Negative scalar must have enclosing parens\n");
// return atof(termStr.c_str());
// }
// if (c != '(' || termStr[l-1] != ')')
// err("CanonicalExpression: expected enclosing parens\n");
// String tmp = termStr.substr(1, l-2);
// return findFactor(tmp, true, dummy);
// }
// ComplexOrRealType findFactor(String termStr,
// bool prevHadParens,
// std::complex<RealType>*) const
// {
// std::cerr<<"WARNING: CanonicalExpression: ";
// std::cerr<<"Complex scalars not yet implemented (sorry)\n";
// RealType *x = 0;
// return findFactor(termStr, prevHadParens, x);
// }
const ItemSpecType& itemSpec_;
};
} // namespace PsimagLite
......
......@@ -30,7 +30,8 @@ public:
buffer += t[i];
}
value_ = PsimagLite::atof(buffer);
if (isImag_ && buffer == "") value_ = 1;
else value_ = PsimagLite::atof(buffer);
}
ComplexType value() const
......
......@@ -103,6 +103,43 @@ public:
return sum;
}
static bool isPureComplex(String t)
{
if (t == "i") return true;
const SizeType n = t.length();
if (n < 2) return false;
String tmp = t.substr(0, n - 1);
return isRealScalar(tmp);
}
static ComplexOrRealType pureComplex(String t)
{
static const bool isComplex = IsComplexNumber<ComplexOrRealType>::True;
if (!isComplex)
err("i = sqrt(-1) found in code path that is real\n");
CpmlxOrReal<RealType, (isComplex) ? 1 : 0> cmplxOrReal(t);
return cmplxOrReal.value();
}
static bool isRealScalar(String termStr)
{
const SizeType n = termStr.length();
if (n == 0)
err("CanonicalExpression: term must not be empty\n");
for (SizeType i = 0; i < n; ++i) {
char c = termStr[i];
bool isDigit = (c >= '0' && c <= '9');
if (c == '.' || c == '-' || c == '+' || isDigit)
continue;
return false;
}
return true;
}
private:
static ComplexOrRealType pureRealOrPureImag(String t)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment