Commit ac07897b authored by Alvarez, Gonzalo's avatar Alvarez, Gonzalo
Browse files

ExpressionCalculator now works for complex

parent 9dff561b
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
#include "ExpressionCalculator.h"

#ifdef USE_COMPLEX
typedef std::complex<double> ComplexOrRealType;
#else
typedef double ComplexOrRealType;
#endif

int main(int argc, char **argv)
{
	if (argc < 2) return 1;

	typedef PsimagLite::ExpressionCalculator<double> ExpressionCalculatorType;
	typedef PsimagLite::PrepassData<double> PrepassDataType;
	typedef PsimagLite::ExpressionCalculator<ComplexOrRealType> ExpressionCalculatorType;
	typedef PsimagLite::PrepassData<ComplexOrRealType> PrepassDataType;

	ExpressionCalculatorType::VectorStringType ve;
	PsimagLite::split(ve, argv[1], ",");
+3 −2
Original line number Diff line number Diff line
@@ -76,9 +76,10 @@ class ExpressionCalculator {
			for (SizeType i = 0; i < 4; ++i) op[i] = '\0';
			SizeType l = str.size();
			if (l == 0) return;
			if (isAfloat(str)) {
			IsAnumberPossiblyComplex<ComplexOrRealType> isAnumberPossiblyComplex(str);
			if (isAnumberPossiblyComplex()) {
				type = NODE_NUMBER;
				value = atof(str.c_str());
				value = isAnumberPossiblyComplex.value();
			} else {
				ary = findAry(str);
				for (SizeType i = 0; i < 4; ++i)
+85 −0
Original line number Diff line number Diff line
@@ -132,6 +132,91 @@ private:
	String cmdLine_;
	String microArch_;
};

template<typename ComplexOrRealType, bool isComplex = IsComplexNumber<ComplexOrRealType>::True>
class IsAnumberPossiblyComplex {};


template<typename ComplexOrRealType>
class IsAnumberPossiblyComplex<ComplexOrRealType, false> {
public:

	IsAnumberPossiblyComplex(String str)
	    : flag_(isAfloat(str)), value_(0)
	{
		if (flag_) value_ = PsimagLite::atof(str.c_str());
	}

	bool operator()() const { return flag_; }

	ComplexOrRealType value() const { return value_; }

private:

	bool flag_;
	ComplexOrRealType value_;
};

template<typename ComplexOrRealType>
class IsAnumberPossiblyComplex<ComplexOrRealType, true> {

public:

	IsAnumberPossiblyComplex(String str)
	    : flag_(false), value_(0)
	{
		// (a, b) or (a,b)
		SizeType l = str.length();
		if (l < 5) {
			seeIfItsAfloat(str);
			return;
		}

		if (str[0] != '(') {
			seeIfItsAfloat(str);
			return;
		}

		String buffer;
		String realPart;
		String imagPart;
		for (SizeType i = 1; i < l; ++i) {
			if (str[i] == ',') {
				realPart = buffer;
				buffer = "";
			} else if (str[i] == ')') {
				imagPart = buffer;
				buffer = "";
			} else if (str[i] != ' ') {
				buffer += str[i];
			} else {
				flag_ = false;
				return;
			}
		}

		flag_ = (isAfloat(realPart) && isAfloat(imagPart));
		if (!flag_) return;
		value_ = ComplexOrRealType(PsimagLite::atof(realPart), PsimagLite::atof(imagPart));
 	}

	bool operator()() const { return flag_; }

	ComplexOrRealType value() const { return value_; }

private:

	void seeIfItsAfloat(String str)
	{
		flag_ = isAfloat(str);
		if (!flag_) return;
		value_ = PsimagLite::atof(str);
	}

	bool flag_;
	ComplexOrRealType value_;
};

} // namespace PsimagLite

void err(PsimagLite::String);