All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scudata.expression.fn.algebra.Inverse Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20241126
Show newest version
package com.scudata.expression.fn.algebra;

import com.scudata.common.Logger;
import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.dm.Sequence;
import com.scudata.expression.Function;
import com.scudata.resources.EngineMessage;

/**
 * ????????inverse(A)???棬ֻ?з???????ʱ???????, inverse@p(A)α????????о???A????α?????B??????ABA=B??BAB=A???޽?ʱ????С???˷?
 * @author bd
 *
 */
public class Inverse extends Function{
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("inverse" + mm.getMessage("function.missingParam"));
		} else if (!param.isLeaf()) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("inverse" + mm.getMessage("function.invalidParam"));
		}
	}

	public Object calculate(Context ctx) {
		Object result1 = param.getLeafExpression().calculate(ctx);
		if (!(result1 instanceof Sequence)) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("inverse" + mm.getMessage("function.paramTypeError"));
		}
		Matrix A = new Matrix((Sequence) result1);
		if (A.getCols() == 0 || A.getRows() == 0) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("inverse" + mm.getMessage("function.paramTypeError"));
		}
		Matrix X = null;
		boolean pseudo = false;
		boolean auto = false;
		if (option != null) {
			if (option.indexOf('a') > -1) {
				auto = true;
			}
			else if (option.indexOf('p') > -1) {
				pseudo = true;
			}
		}
		try {
			if (pseudo) {
				X = A.pseudoinverse();
			}
			else {
				X = A.inverse();
			}
		}
		catch (Exception e) {
			if (auto) {
				// A?޷?????ʱ????α?????
				X = A.pseudoinverse();
			}
			else {
				Logger.warn("inverse error: " + e.getMessage());
				return null;
			}
		}
		if (X == null) {
			return null;
		}
		return X.toSequence(option, false);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy