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

com.scudata.expression.fn.math.Gcd Maven / Gradle / Ivy

Go to download

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

There is a newer version: 20240823
Show newest version
package com.scudata.expression.fn.math;

import java.util.ArrayList;
import java.util.Arrays;

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.expression.IParam;
import com.scudata.resources.EngineMessage;
import com.scudata.util.Variant;

/**
 * ?????г?Ա?????????????Լ??,????ֵ??Ա?????ԣ???ֵ??Ա???Զ?ȡ??,????С??0?ij?Ա???ش???ֵ0
 * @author yanjing
 *
 */
public class Gcd extends Function {
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("gcd" + mm.getMessage("function.missingParam"));
		}
	}

	public Object calculate(Context ctx) {
		ArrayList num=new ArrayList();
		if(param.isLeaf()) {
			Object result = param.getLeafExpression().calculate(ctx);
			if (result != null && result instanceof Number) {
				num.add((Number)result);
			} else if (result != null && result instanceof Sequence) {
				int n=((Sequence)result).length();
				for (int i=1;i<=n;i++) {
					Object tmp=((Sequence)result).get(i);
					if (tmp!=null && tmp instanceof Number) {
						num.add((Number)tmp);
					}
				}
			}
		} else {
			int size = param.getSubSize();
			
			for (int j=0; j max) {
			long tmp=min;
			min=max;
			max=tmp;
		}
		
		if(min==0) {
			return max;//??Ϊ0?ܱ??κ????????????ݰٶȰٿ?
		} else {
			return gcd(min,max%min);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy