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

org.openl.codegen.template.MathUtils-functions.vm Maven / Gradle / Ivy

There is a newer version: 5.19.9
Show newest version
	// MAX function
#foreach ($type in $primitiveTypes)
	public static boolean max($type value1, $type value2) {
	    return value1 > value2;
	}
#end

	// MAX IN ARRAY function
#foreach ($type in $primitiveTypes)
	public static $type max($type[] values) {
        return NumberUtils.max(values);
    }
#end
	
	// MIN function 
#foreach ($type in $primitiveTypes)
	public static boolean min($type value1, $type value2) {
        return value1 < value2;
    }
#end
	
	// MIN IN ARRAY function
#foreach ($type in $primitiveTypes)
	public static $type min($type[] values) {
        return NumberUtils.min(values);
    }
#end    

	// AVERAGE
#foreach ($type in $primitiveTypes)
	public static $type avg($type[] values) {
        if (!ArrayUtils.isEmpty(values)) {
            return ($type) (sum(values)/values.length);
        }
        return 0;
    }
#end
	// AVERAGE for wrapper types
#foreach ($type in $wrapperTypes)
	public static $tool.getTypeName($type) avg($tool.getTypeName($type)[] values) {
        int valuableSize = ArrayTool.getNotNullValuesCount(values);
        if (valuableSize == 0) {
            return ${tool.getTypeName($type)}.valueOf("0");
        }
        
        return ($tool.getNumericPrimitive($type)) (sum(values) / 
            ${tool.getTypeName($type)}.valueOf(($tool.getNumericPrimitive($type)) valuableSize));
    }
#end
    // AVERAGE for big numeric types
#foreach ($type in $bigNumericTypes)
    public static $tool.getTypeName($type) avg($tool.getTypeName($type)[] values) {
        int valuableSize = ArrayTool.getNotNullValuesCount(values);
        if (valuableSize == 0) {
            return ${tool.getTypeName($type)}.ZERO;
        }
        
        return divide(sum(values), ${tool.getTypeName($type)}.valueOf(valuableSize));
    }
#end

	// SMALL for primitives	
#foreach ($type in $primitiveTypes)	
    public static $type small($type[] values, int position) {
        $type result = 0;
        int index = position - 1; // arrays are 0-based
        if (values == null) {
            throw new IllegalArgumentException("The array cannot be null");
        }
        if (index < 0 || values.length <= index) {
            throw new IllegalArgumentException(String.format("There is no position '%d' in the given array", position));
        }
        Arrays.sort(values);
        result = values[index];
        
        return result;
    }
#end   

    // SMALL for wrapper types 
#foreach ($type in $wrapperTypes)
	public static $tool.getTypeName($type) small($tool.getTypeName($type)[] values, int position) {
       $tool.getTypeName($type) result = ${tool.getTypeName($type)}.valueOf("0");
       int index = position - 1; 
       if (values == null) {
           throw new IllegalArgumentException("The array cannot be null");
       }
       values = ArrayTool.removeNulls(values);
       if (index < 0 || values.length <= index) {
           throw new IllegalArgumentException(String.format("There is no position '%d' in the given array", position));
       }        
       Arrays.sort(values);
       result = values[index];
       
       return result;
    }
#end 

    // SMALL for big numeric types
#foreach ($type in $bigNumericTypes)
    public static $tool.getTypeName($type) small($tool.getTypeName($type)[] values, int position) {
        $tool.getTypeName($type) result = ${tool.getTypeName($type)}.ZERO;
        int index = position - 1; 
        if (values == null) {
            throw new IllegalArgumentException("The array cannot be null");
        }
        values = ArrayTool.removeNulls(values);
        if (index < 0 || values.length <= index) {
            throw new IllegalArgumentException(String.format("There is no position '%d' in the given array", position));
        }
        Arrays.sort(values);
        result = values[index];
        
        return result;
    }
#end 

	// BIG for primitives	
#foreach ($type in $primitiveTypes)	
    public static $type big($type[] values, int position) {
        $type result = 0;
        int index = position - 1; // arrays are 0-based
        if (values == null) {
            throw new IllegalArgumentException("The array cannot be null");
        }
        if (index < 0 || values.length <= index) {
            throw new IllegalArgumentException(String.format("There is no position '%d' in the given array", position));
        }
        Arrays.sort(values);
        result = values[values.length - 1 - index];
        
        return result;
    }
#end   

    // BIG for wrapper types
#foreach ($type in $wrapperTypes)
	public static $tool.getTypeName($type) big($tool.getTypeName($type)[] values, int position) {
       $tool.getTypeName($type) result = ${tool.getTypeName($type)}.valueOf("0");
       int index = position - 1; 
       if (values == null) {
           throw new IllegalArgumentException("The array cannot be null");
       }
       values = ArrayTool.removeNulls(values);
       if (index < 0 || values.length <= index) {
           throw new IllegalArgumentException(String.format("There is no position '%d' in the given array", position));
       }        
       Arrays.sort(values);
       result = values[values.length - 1 - index];
       
       return result;
    }
#end

    // BIG for big numeric types
#foreach ($type in $bigNumericTypes)
    public static $tool.getTypeName($type) big($tool.getTypeName($type)[] values, int position) {
        $tool.getTypeName($type) result = ${tool.getTypeName($type)}.ZERO;
        int index = position - 1; 
        if (values == null) {
            throw new IllegalArgumentException("The array cannot be null");
        }
        values = ArrayTool.removeNulls(values);
        if (index < 0 || values.length <= index) {
            throw new IllegalArgumentException(String.format("There is no position '%d' in the given array", position));
        }
        Arrays.sort(values);
        result = values[values.length - 1 - index];
       
        return result;
    }
#end

	// SUM
#foreach ($type in $primitiveTypes)
#if ($tool.isNotPrimitiveDouble($type)) 
	public static $type sum($type[] values) {
        return ($type) sum(${type}ArrayToDouble(values));        
    } 
#end
#end  

	 // SUM for wrapper types
#foreach ($type in $wrapperTypes)
	public static $tool.getTypeName($type) sum($tool.getTypeName($type)[] values) {
        $tool.getNumericPrimitive($type) sum = ${tool.getTypeName($type)}.valueOf("0");
        for ($tool.getTypeName($type) value : values) {
            if (value != null) {
                sum += value;
            }
        }
        return sum;
    }
#end

     // SUM for big numeric types
#foreach ($type in $bigNumericTypes)
    public static $tool.getTypeName($type) sum($tool.getTypeName($type)[] values) {
        $tool.getTypeName($type) sum = ${tool.getTypeName($type)}.ZERO;
        for ($tool.getTypeName($type) value : values) {
            if (value != null) {
                sum = sum.add(value);
            }
        }
        return sum;
    }
#end

	// MEDIAN
#foreach ($type in $primitiveTypes)
#if ($tool.isNotPrimitiveDouble($type)) 
	public static $type median($type[] values) {
        double[] doubleArray = ${type}ArrayToDouble(values);
        
        Median median = new Median();
        return ($type) median.evaluate(doubleArray, 0, doubleArray.length);
    }
#end
#end

	// MEDIAN for all wrapper types
#foreach ($type in $allWrapperTypes)
	public static $tool.getTypeName($type) median($tool.getTypeName($type)[] values) {    
        // TODO implement
        throw new NotImplementedException(String.format("Method median for %s is not implemented yet", 
            values.getClass().getName()));
    }
#end

	// PRODUCT
#foreach ($type in $primitiveTypes)
#if ($tool.isNotPrimitiveDouble($type))
	public static double product(${type}[] values) {
        return product(${type}ArrayToDouble(values));
    }
#end
#end   

	// PRODUCT
#foreach ($type in $primitiveTypes)
#if ($tool.isNotPrimitiveDouble($type))
	private static double[] ${type}ArrayToDouble($type[] values) {        
        double[] doubleArray = new double[values.length];        
        for (int i = 0; i < values.length; i++) {
            doubleArray[i] = (double) values[i];
        }
        return doubleArray;
    }
#end
#end
	
	// PRODUCT for wrapper types
#foreach ($type in $wrapperTypes)
	public static double product($tool.getTypeName($type)[] values) {
        boolean hasValues = false;
        
        double res = 1;
        for ($tool.getTypeName($type) value : values) {
            if (value != null) {
                res *= value;
                hasValues = true;
            }
        }
        
        return hasValues ? res : 0;
    }
#end
    
    // PRODUCT for big numeric types
#foreach ($type in $bigNumericTypes)
    public static $tool.getTypeName($type) product($tool.getTypeName($type)[] values) {
        boolean hasValues = false;
        
        $tool.getTypeName($type) res = ${tool.getTypeName($type)}.ONE;
        for ($tool.getTypeName($type) value : values) {
            if (value != null) {
                res = res.multiply(value);
                hasValues = true;
            }
        }
        
        return hasValues ? res : ${tool.getTypeName($type)}.ZERO;
    }
#end

	// SLICE function
#foreach ($type in $primitiveTypes)
	public static $type[] slice($type[] values, int startIndexInclusive) {
        if (values != null) {
            return slice(values, startIndexInclusive, values.length);
        }
        return null;
    }
    
    public static $type[] slice($type[] values, int startIndexInclusive, int endIndexExclusive) {
        if (values != null) {
            return ArrayUtils.subarray(values, startIndexInclusive, endIndexExclusive);
        }
        return null;
    } 
    
#end

	// SLICE for all wrapper types
#foreach ($type in $allWrapperTypes)
	public static $tool.getTypeName($type)[] slice($tool.getTypeName($type)[] values, int startIndexInclusive) {
        if (values != null) {
            return slice(values, startIndexInclusive, values.length);
        }
        return null;
    }
    
    public static $tool.getTypeName($type)[] slice($tool.getTypeName($type)[] values, int startIndexInclusive, int endIndexExclusive) {
        if (values != null) {
            return ($tool.getTypeName($type)[]) ArrayUtils.subarray(values, startIndexInclusive, endIndexExclusive);
        }
        return null;
    }
#end
	
	// MOD is implemented as in Excel.
#foreach ($type in $primitiveTypes)
	public static $type mod($type number, $type divisor) {        
        long quotient = quotient(number, divisor);
        
        $type intPart = ($type) quotient;
        if (quotient < 0) {            
            intPart--;
        } 
        return ($type) (number - intPart * divisor);
    }
#end
	
	// MOD for wrapper types
#foreach ($type in $wrapperTypes)
	public static $tool.getTypeName($type) mod($tool.getTypeName($type) number, $tool.getTypeName($type) divisor) {   
        if (number == null || divisor == null) {
            return ${tool.getTypeName($type)}.valueOf("0");
        }
        return mod(($tool.getNumericPrimitive($type)) number, ($tool.getNumericPrimitive($type)) divisor);
    }
#end
    
    // MOD for big numeric types
#foreach ($type in $bigNumericTypes)
    public static $tool.getTypeName($type) mod($tool.getTypeName($type) number, $tool.getTypeName($type) divisor) {   
        if (number == null || divisor == null) {
            return ${tool.getTypeName($type)}.ZERO;
        }
        long quotient = quotient(number, divisor);
        
        long intPart = quotient;
        if (quotient < 0) {            
            intPart--;
        }
        return number.subtract(${tool.getTypeName($type)}.valueOf(intPart).multiply(divisor));
    }
#end
	
	// QUAOTIENT
#foreach ($type in $primitiveTypes)	
	public static long quotient($type number, $type divisor) {
        return (long) (number / divisor);
    }
#end

	// QUAOTIENT for wrapper types
#foreach ($type in $wrapperTypes)
	public static long quotient($tool.getTypeName($type) number, $tool.getTypeName($type) divisor) {
        if (number == null || divisor == null) {
            return 0;
        }        
        return quotient(($tool.getNumericPrimitive($type)) number, ($tool.getNumericPrimitive($type)) divisor);
    }
 #end

    // QUAOTIENT for big numeric types
#foreach ($type in $bigNumericTypes)
    public static long quotient($tool.getTypeName($type) number, $tool.getTypeName($type) divisor) {
        if (number == null || divisor == null) {
            return 0;
        }
        return divide(number, divisor).longValue();
    }
 #end
 
 	// SORT
 #foreach ($type in $primitiveTypes)	
	public static $type[] sort($type[] values) {
		Arrays.sort(values);
        return values;
    }
#end

	// SORT for all wrapper types
#foreach ($type in $allWrapperTypes)
	public static $tool.getTypeName($type)[] sort($tool.getTypeName($type)[] values) {
        values = ArrayTool.removeNulls(values);
		Arrays.sort(values);
        return values;
    }
 #end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy