org.jamesii.ml3.parser.buildIns.mathFunctions.CosHFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ml3 Show documentation
Show all versions of ml3 Show documentation
The Modeling Language for Linked Lives, a domain specific modeling language for agent-based
computational demography.
/*
* Copyright 2018 University of Rostock
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jamesii.ml3.parser.buildIns.mathFunctions;
import org.jamesii.ml3.model.types.BasicType;
import org.jamesii.ml3.model.types.IType;
import org.jamesii.ml3.model.validation.typeChecking.Scope;
import org.jamesii.ml3.model.validation.typeChecking.Utils;
import org.jamesii.ml3.model.values.IValue;
import org.jamesii.ml3.model.values.IntValue;
import org.jamesii.ml3.model.values.RealValue;
import org.jamesii.ml3.parser.IParseTreeNode;
import org.jamesii.ml3.parser.buildIns.BuildIns;
import org.jamesii.ml3.simulator.context.IContext;
import org.jamesii.ml3.simulator.exceptions.TypeException;
import java.util.Collection;
/**
* Represents the "cosh : real -> real" function, hyperbolic cosine.
*/
public class CosHFunction implements IMathFunction {
@Override
public IValue evaluate(Collection parameters, IContext context) {
IValue v = parameters.iterator().next();
if (v instanceof RealValue) {
return new RealValue(Math.cosh(((RealValue) v).getValue()));
} else if (v instanceof IntValue) {
return new RealValue(Math.cosh(((IntValue) v).getValue()));
} else
throw new TypeException("Type error in arguments of " + BuildIns.FUNC_COSH + "."); }
@Override
public IType getType(Collection parameters, Scope scope, IParseTreeNode currentNode) {
if(!Utils.checkParameterSize(scope, parameters, 1, currentNode)){
return BasicType.UNKNOWN;
}
if (!Utils.checkNumeric(scope, currentNode, parameters.iterator().next())){
return BasicType.UNKNOWN;
}
return BasicType.REAL;
}
@Override
public String getName() {
return BuildIns.FUNC_COSH;
}
}