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

com.scudata.expression.fn.algebra.Var 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.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;

/**
 * ????V?????巽??, ʹ??@sѡ??ʱ????????????
 * @author bd
 */
public class Var extends Function{
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("var" + mm.getMessage("function.missingParam"));
		} else if (!param.isLeaf()) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("var" + 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("var" + mm.getMessage("function.paramTypeError"));
		}
		Sequence ser = (Sequence) result1;
		boolean statistics = option != null && option.indexOf('s') > -1;
		return var(ser, statistics);
	}
	
	protected static double var(Sequence ser, boolean sta) {
		Object avg = ser.average();
		if (avg instanceof Number) {
			double avgValue = ((Number) avg).doubleValue();
			int n = ser.length();
			double result = 0;
			for(int i = 1; i <= n; i++){
				Number tmp = (Number) ser.get(i);
				double v = tmp == null ? 0 : tmp.doubleValue();
				if (tmp!=null){
					result+=Math.pow(v-avgValue, 2);
				}
			}
			if (sta) {
				return result / (n - 1);
			} else {
				return result / n;
			}
		}
		return 0d;		
	}
	
	protected static double var(double[] vs, boolean sta) {
		double sum = 0d;
		int n = vs.length;
		if (n < 1) return 0;
		for (int i = 0; i < n; i++) {
			sum += vs[i];
		}
		double avg = sum/n;
		double result = 0;
		for(int i = 0; i < n; i++){
			result+=Math.pow(vs[i]-avg, 2);
		}
		if (sta) {
			return result / (n - 1);
		} else {
			return result / n;
		}
	}
	
	protected static double std(Sequence ser, boolean sta) {
		double var = var(ser, sta);
		return Math.sqrt(var);
	}
	
	protected static double std(double[] vs, boolean sta) {
		double var = var(vs, sta);
		return Math.sqrt(var);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy