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

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

/**
 * ????ת??
 * @author bd
 *
 */
public class Transpose extends Function{
	/**
	 * ??????ʽ????Ч?ԣ???Ч???׳??쳣
	 */
	public void checkValidity() {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("transpose" + mm.getMessage("function.missingParam"));
		} else if (!param.isLeaf()) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("transpose" + 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("transpose" + mm.getMessage("function.paramTypeError"));
		}
		// ??????????ת?ã????ı??Ա???????ͣ???ת?ú????һ??????????ȱʧֵ??0d
		// edited by bd, 2023.10.23, ????ѡ??@n, ????ת?ú?ʹ?ÿ?ֵ
		boolean ifn = this.option != null && this.option.indexOf('n') > -1; 
		Sequence seq = (Sequence) result1;
		int rows = seq.length();
		int cols = 0;
		for (int r = 1; r <= rows; r++) {
			Object o = seq.get(r);
			if (o instanceof Sequence) {
				cols = Math.max(cols, ((Sequence) o).length());
			}
		}
		Double zero = Double.valueOf(0);
		// edited by bd, 2023.10.23, ????ѡ??@n, ????ת?ú?ʹ?ÿ?ֵ
		if (ifn) zero = null;
		if (cols == 0) {
			Sequence result = new Sequence(rows);
			// һλ???У?ֻ??ת????ʽ????
			for (int r = 1; r <= rows; r++ ) {
				Object o = seq.get(r);
				Sequence sub = new Sequence(1);
				sub.add(o);
				result.add(sub);
			}
			return result;
		}
		Sequence result = new Sequence(cols);
		for (int c = 1; c <= cols; c++) {
			Sequence sub = new Sequence(rows);
			for (int r = 1; r <= rows; r++) {
				Object o = seq.get(r);
				if (o instanceof Sequence) {
					Sequence subSeq = (Sequence) o;
					if (subSeq.length() >= c) {
						o = subSeq.get(c);
					}
					// edited by bd, 2023.10.23, ???ﴦ?????????⣬??????????????г??Ȳ???ʱ??Ӧ????Ϊnull
					else {
						o = zero;
					}
				}
				else if (c > 1) {
					o = zero;
				}
				sub.add(o);
			}
			result.add(sub);
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy