Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
interpreter = interpreters.PythonInterpreter()
expected_code = """
import numpy as np
def score(input):
return (np.asarray([1, 2])) * (1)
"""
utils.assert_code_equal(interpreter.interpret(expr), expected_code)
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
interpreter = interpreters.JavaInterpreter()
expected_code = """
public class Model {
public static double[] score(double[] input) {
return mulVectorNumber(new double[] {1, 2}, 1);
}
public static double[] addVectors(double[] v1, double[] v2) {
double[] result = new double[v1.length];
for (int i = 0; i < v1.length; i++) {
result[i] = v1[i] + v2[i];
ast.BinVectorNumExpr(
ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(1.5),
ast.CompOpType.LTE),
ast.VectorVal([
ast.NumVal(0.0),
ast.NumVal(1.0)]),
ast.VectorVal([
ast.NumVal(1.0),
ast.NumVal(0.0)]))),
ast.NumVal(0.5),
ast.BinNumOpType.MUL),
ast.BinVectorNumExpr(
ast.SubroutineExpr(
ast.IfExpr(
ast.CompExpr(
ast.FeatureRef(0),
ast.NumVal(2.5),
ast.CompOpType.LTE),
ast.VectorVal([
ast.NumVal(1.0),
ast.NumVal(0.0)]),
ast.VectorVal([
ast.NumVal(0.0),
ast.NumVal(1.0)]))),
ast.NumVal(0.5),
ast.BinNumOpType.MUL),
ast.BinNumOpType.ADD)
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
interpreter = interpreters.CInterpreter()
expected_code = """
#include
void add_vectors(double *v1, double *v2, int size, double *result) {
for(int i = 0; i < size; ++i)
result[i] = v1[i] + v2[i];
}
void mul_vector_number(double *v1, double num, int size, double *result) {
for(int i = 0; i < size; ++i)
result[i] = v1[i] * num;
}
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
expected_code = """
Module Model
Function addVectors(ByRef v1() As Double, ByRef v2() As Double) As Double()
Dim resLength As Integer
resLength = UBound(v1) - LBound(v1)
Dim result() As Double
ReDim result(resLength)
Dim i As Integer
For i = LBound(v1) To UBound(v1)
result(i) = v1(i) + v2(i)
Next i
def test_bin_vector_num_expr():
expr = ast.BinVectorNumExpr(
ast.VectorVal([ast.NumVal(1), ast.NumVal(2)]),
ast.NumVal(1),
ast.BinNumOpType.MUL)
interpreter = interpreters.GoInterpreter()
expected_code = """
func addVectors(v1, v2 []float64) []float64 {
result := make([]float64, len(v1))
for i := 0; i < len(v1); i++ {
result[i] = v1[i] + v2[i]
}
return result
}
func mulVectorNumber(v1 []float64, num float64) []float64 {
result := make([]float64, len(v1))
def add(l, r, to_reuse=False):
return ast.BinNumExpr(l, r, ast.BinNumOpType.ADD, to_reuse=to_reuse)
def sub(l, r, to_reuse=False):
return ast.BinNumExpr(l, r, ast.BinNumOpType.SUB, to_reuse=to_reuse)
def lte(l, r):
return ast.CompExpr(l, r, ast.CompOpType.LTE)
BIN_EXPR_CLASSES = {
(False, False): ast.BinNumExpr,
(True, True): ast.BinVectorExpr,
(True, False): ast.BinVectorNumExpr,
}
def apply_bin_op(left, right, op):
"""
Finds binary expression class suitable for combination of left and right
expressions depending on whether their output is scalar or vector and
creates instance of this expression with specified operation.
"""
exr_class = BIN_EXPR_CLASSES.get(
(left.output_size > 1, right.output_size > 1))
if exr_class is None:
# change the positions of left and right
left, right = right, left
exr_class = ast.BinVectorNumExpr
def apply_bin_op(left, right, op):
"""
Finds binary expression class suitable for combination of left and right
expressions depending on whether their output is scalar or vector and
creates instance of this expression with specified operation.
"""
exr_class = BIN_EXPR_CLASSES.get(
(left.output_size > 1, right.output_size > 1))
if exr_class is None:
# change the positions of left and right
left, right = right, left
exr_class = ast.BinVectorNumExpr
return exr_class(left, right, op)