1from sympy.core import (pi, symbols, Rational, Integer, GoldenRatio, EulerGamma,
2                        Catalan, Lambda, Dummy, Eq, Ne, Le, Lt, Gt, Ge)
3from sympy.functions import Piecewise, sin, cos, Abs, exp, ceiling, sqrt
4from sympy.testing.pytest import raises, warns_deprecated_sympy
5from sympy.printing.glsl import GLSLPrinter
6from sympy.printing.str import StrPrinter
7from sympy.utilities.lambdify import implemented_function
8from sympy.tensor import IndexedBase, Idx
9from sympy.matrices import Matrix, MatrixSymbol
10from sympy.core import Tuple
11from sympy import glsl_code
12import textwrap
13
14x, y, z = symbols('x,y,z')
15
16
17def test_printmethod():
18    assert glsl_code(Abs(x)) == "abs(x)"
19
20def test_print_without_operators():
21    assert glsl_code(x*y,use_operators = False) == 'mul(x, y)'
22    assert glsl_code(x**y+z,use_operators = False) == 'add(pow(x, y), z)'
23    assert glsl_code(x*(y+z),use_operators = False) == 'mul(x, add(y, z))'
24    assert glsl_code(x*(y+z),use_operators = False) == 'mul(x, add(y, z))'
25    assert glsl_code(x*(y+z**y**0.5),use_operators = False) == 'mul(x, add(y, pow(z, sqrt(y))))'
26    assert glsl_code(-x-y, use_operators=False, zero='zero()') == 'sub(zero(), add(x, y))'
27    assert glsl_code(-x-y, use_operators=False) == 'sub(0.0, add(x, y))'
28
29def test_glsl_code_sqrt():
30    assert glsl_code(sqrt(x)) == "sqrt(x)"
31    assert glsl_code(x**0.5) == "sqrt(x)"
32    assert glsl_code(sqrt(x)) == "sqrt(x)"
33
34
35def test_glsl_code_Pow():
36    g = implemented_function('g', Lambda(x, 2*x))
37    assert glsl_code(x**3) == "pow(x, 3.0)"
38    assert glsl_code(x**(y**3)) == "pow(x, pow(y, 3.0))"
39    assert glsl_code(1/(g(x)*3.5)**(x - y**x)/(x**2 + y)) == \
40        "pow(3.5*2*x, -x + pow(y, x))/(pow(x, 2.0) + y)"
41    assert glsl_code(x**-1.0) == '1.0/x'
42
43
44def test_glsl_code_Relational():
45    assert glsl_code(Eq(x, y)) == "x == y"
46    assert glsl_code(Ne(x, y)) == "x != y"
47    assert glsl_code(Le(x, y)) == "x <= y"
48    assert glsl_code(Lt(x, y)) == "x < y"
49    assert glsl_code(Gt(x, y)) == "x > y"
50    assert glsl_code(Ge(x, y)) == "x >= y"
51
52
53def test_glsl_code_constants_mathh():
54    assert glsl_code(exp(1)) == "float E = 2.71828183;\nE"
55    assert glsl_code(pi) == "float pi = 3.14159265;\npi"
56    # assert glsl_code(oo) == "Number.POSITIVE_INFINITY"
57    # assert glsl_code(-oo) == "Number.NEGATIVE_INFINITY"
58
59
60def test_glsl_code_constants_other():
61    assert glsl_code(2*GoldenRatio) == "float GoldenRatio = 1.61803399;\n2*GoldenRatio"
62    assert glsl_code(2*Catalan) == "float Catalan = 0.915965594;\n2*Catalan"
63    assert glsl_code(2*EulerGamma) == "float EulerGamma = 0.577215665;\n2*EulerGamma"
64
65
66def test_glsl_code_Rational():
67    assert glsl_code(Rational(3, 7)) == "3.0/7.0"
68    assert glsl_code(Rational(18, 9)) == "2"
69    assert glsl_code(Rational(3, -7)) == "-3.0/7.0"
70    assert glsl_code(Rational(-3, -7)) == "3.0/7.0"
71
72
73def test_glsl_code_Integer():
74    assert glsl_code(Integer(67)) == "67"
75    assert glsl_code(Integer(-1)) == "-1"
76
77
78def test_glsl_code_functions():
79    assert glsl_code(sin(x) ** cos(x)) == "pow(sin(x), cos(x))"
80
81
82def test_glsl_code_inline_function():
83    x = symbols('x')
84    g = implemented_function('g', Lambda(x, 2*x))
85    assert glsl_code(g(x)) == "2*x"
86    g = implemented_function('g', Lambda(x, 2*x/Catalan))
87    assert glsl_code(g(x)) == "float Catalan = 0.915965594;\n2*x/Catalan"
88    A = IndexedBase('A')
89    i = Idx('i', symbols('n', integer=True))
90    g = implemented_function('g', Lambda(x, x*(1 + x)*(2 + x)))
91    assert glsl_code(g(A[i]), assign_to=A[i]) == (
92        "for (int i=0; i<n; i++){\n"
93        "   A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n"
94        "}"
95    )
96
97
98def test_glsl_code_exceptions():
99    assert glsl_code(ceiling(x)) == "ceil(x)"
100    assert glsl_code(Abs(x)) == "abs(x)"
101
102
103def test_glsl_code_boolean():
104    assert glsl_code(x & y) == "x && y"
105    assert glsl_code(x | y) == "x || y"
106    assert glsl_code(~x) == "!x"
107    assert glsl_code(x & y & z) == "x && y && z"
108    assert glsl_code(x | y | z) == "x || y || z"
109    assert glsl_code((x & y) | z) == "z || x && y"
110    assert glsl_code((x | y) & z) == "z && (x || y)"
111
112
113def test_glsl_code_Piecewise():
114    expr = Piecewise((x, x < 1), (x**2, True))
115    p = glsl_code(expr)
116    s = \
117"""\
118((x < 1) ? (
119   x
120)
121: (
122   pow(x, 2.0)
123))\
124"""
125    assert p == s
126    assert glsl_code(expr, assign_to="c") == (
127    "if (x < 1) {\n"
128    "   c = x;\n"
129    "}\n"
130    "else {\n"
131    "   c = pow(x, 2.0);\n"
132    "}")
133    # Check that Piecewise without a True (default) condition error
134    expr = Piecewise((x, x < 1), (x**2, x > 1), (sin(x), x > 0))
135    raises(ValueError, lambda: glsl_code(expr))
136
137
138def test_glsl_code_Piecewise_deep():
139    p = glsl_code(2*Piecewise((x, x < 1), (x**2, True)))
140    s = \
141"""\
1422*((x < 1) ? (
143   x
144)
145: (
146   pow(x, 2.0)
147))\
148"""
149    assert p == s
150
151
152def test_glsl_code_settings():
153    raises(TypeError, lambda: glsl_code(sin(x), method="garbage"))
154
155
156def test_glsl_code_Indexed():
157    from sympy.tensor import IndexedBase, Idx
158    from sympy import symbols
159    n, m, o = symbols('n m o', integer=True)
160    i, j, k = Idx('i', n), Idx('j', m), Idx('k', o)
161    p = GLSLPrinter()
162    p._not_c = set()
163
164    x = IndexedBase('x')[j]
165    assert p._print_Indexed(x) == 'x[j]'
166    A = IndexedBase('A')[i, j]
167    assert p._print_Indexed(A) == 'A[%s]' % (m*i+j)
168    B = IndexedBase('B')[i, j, k]
169    assert p._print_Indexed(B) == 'B[%s]' % (i*o*m+j*o+k)
170
171    assert p._not_c == set()
172
173def test_glsl_code_list_tuple_Tuple():
174    assert glsl_code([1,2,3,4]) == 'vec4(1, 2, 3, 4)'
175    assert glsl_code([1,2,3],glsl_types=False) == 'float[3](1, 2, 3)'
176    assert glsl_code([1,2,3]) == glsl_code((1,2,3))
177    assert glsl_code([1,2,3]) == glsl_code(Tuple(1,2,3))
178
179    m = MatrixSymbol('A',3,4)
180    assert glsl_code([m[0],m[1]])
181
182def test_glsl_code_loops_matrix_vector():
183    n, m = symbols('n m', integer=True)
184    A = IndexedBase('A')
185    x = IndexedBase('x')
186    y = IndexedBase('y')
187    i = Idx('i', m)
188    j = Idx('j', n)
189
190    s = (
191        'for (int i=0; i<m; i++){\n'
192        '   y[i] = 0.0;\n'
193        '}\n'
194        'for (int i=0; i<m; i++){\n'
195        '   for (int j=0; j<n; j++){\n'
196        '      y[i] = A[n*i + j]*x[j] + y[i];\n'
197        '   }\n'
198        '}'
199    )
200
201    c = glsl_code(A[i, j]*x[j], assign_to=y[i])
202    assert c == s
203
204
205def test_dummy_loops():
206    i, m = symbols('i m', integer=True, cls=Dummy)
207    x = IndexedBase('x')
208    y = IndexedBase('y')
209    i = Idx(i, m)
210
211    expected = (
212        'for (int i_%(icount)i=0; i_%(icount)i<m_%(mcount)i; i_%(icount)i++){\n'
213        '   y[i_%(icount)i] = x[i_%(icount)i];\n'
214        '}'
215    ) % {'icount': i.label.dummy_index, 'mcount': m.dummy_index}
216    code = glsl_code(x[i], assign_to=y[i])
217    assert code == expected
218
219
220def test_glsl_code_loops_add():
221    from sympy.tensor import IndexedBase, Idx
222    from sympy import symbols
223    n, m = symbols('n m', integer=True)
224    A = IndexedBase('A')
225    x = IndexedBase('x')
226    y = IndexedBase('y')
227    z = IndexedBase('z')
228    i = Idx('i', m)
229    j = Idx('j', n)
230
231    s = (
232        'for (int i=0; i<m; i++){\n'
233        '   y[i] = x[i] + z[i];\n'
234        '}\n'
235        'for (int i=0; i<m; i++){\n'
236        '   for (int j=0; j<n; j++){\n'
237        '      y[i] = A[n*i + j]*x[j] + y[i];\n'
238        '   }\n'
239        '}'
240    )
241    c = glsl_code(A[i, j]*x[j] + x[i] + z[i], assign_to=y[i])
242    assert c == s
243
244
245def test_glsl_code_loops_multiple_contractions():
246    from sympy.tensor import IndexedBase, Idx
247    from sympy import symbols
248    n, m, o, p = symbols('n m o p', integer=True)
249    a = IndexedBase('a')
250    b = IndexedBase('b')
251    y = IndexedBase('y')
252    i = Idx('i', m)
253    j = Idx('j', n)
254    k = Idx('k', o)
255    l = Idx('l', p)
256
257    s = (
258        'for (int i=0; i<m; i++){\n'
259        '   y[i] = 0.0;\n'
260        '}\n'
261        'for (int i=0; i<m; i++){\n'
262        '   for (int j=0; j<n; j++){\n'
263        '      for (int k=0; k<o; k++){\n'
264        '         for (int l=0; l<p; l++){\n'
265        '            y[i] = a[%s]*b[%s] + y[i];\n' % (i*n*o*p + j*o*p + k*p + l, j*o*p + k*p + l) +\
266        '         }\n'
267        '      }\n'
268        '   }\n'
269        '}'
270    )
271    c = glsl_code(b[j, k, l]*a[i, j, k, l], assign_to=y[i])
272    assert c == s
273
274
275def test_glsl_code_loops_addfactor():
276    from sympy.tensor import IndexedBase, Idx
277    from sympy import symbols
278    n, m, o, p = symbols('n m o p', integer=True)
279    a = IndexedBase('a')
280    b = IndexedBase('b')
281    c = IndexedBase('c')
282    y = IndexedBase('y')
283    i = Idx('i', m)
284    j = Idx('j', n)
285    k = Idx('k', o)
286    l = Idx('l', p)
287
288    s = (
289        'for (int i=0; i<m; i++){\n'
290        '   y[i] = 0.0;\n'
291        '}\n'
292        'for (int i=0; i<m; i++){\n'
293        '   for (int j=0; j<n; j++){\n'
294        '      for (int k=0; k<o; k++){\n'
295        '         for (int l=0; l<p; l++){\n'
296        '            y[i] = (a[%s] + b[%s])*c[%s] + y[i];\n' % (i*n*o*p + j*o*p + k*p + l, i*n*o*p + j*o*p + k*p + l, j*o*p + k*p + l) +\
297        '         }\n'
298        '      }\n'
299        '   }\n'
300        '}'
301    )
302    c = glsl_code((a[i, j, k, l] + b[i, j, k, l])*c[j, k, l], assign_to=y[i])
303    assert c == s
304
305
306def test_glsl_code_loops_multiple_terms():
307    from sympy.tensor import IndexedBase, Idx
308    from sympy import symbols
309    n, m, o, p = symbols('n m o p', integer=True)
310    a = IndexedBase('a')
311    b = IndexedBase('b')
312    c = IndexedBase('c')
313    y = IndexedBase('y')
314    i = Idx('i', m)
315    j = Idx('j', n)
316    k = Idx('k', o)
317
318    s0 = (
319        'for (int i=0; i<m; i++){\n'
320        '   y[i] = 0.0;\n'
321        '}\n'
322    )
323    s1 = (
324        'for (int i=0; i<m; i++){\n'
325        '   for (int j=0; j<n; j++){\n'
326        '      for (int k=0; k<o; k++){\n'
327        '         y[i] = b[j]*b[k]*c[%s] + y[i];\n' % (i*n*o + j*o + k) +\
328        '      }\n'
329        '   }\n'
330        '}\n'
331    )
332    s2 = (
333        'for (int i=0; i<m; i++){\n'
334        '   for (int k=0; k<o; k++){\n'
335        '      y[i] = a[%s]*b[k] + y[i];\n' % (i*o + k) +\
336        '   }\n'
337        '}\n'
338    )
339    s3 = (
340        'for (int i=0; i<m; i++){\n'
341        '   for (int j=0; j<n; j++){\n'
342        '      y[i] = a[%s]*b[j] + y[i];\n' % (i*n + j) +\
343        '   }\n'
344        '}\n'
345    )
346    c = glsl_code(
347        b[j]*a[i, j] + b[k]*a[i, k] + b[j]*b[k]*c[i, j, k], assign_to=y[i])
348    assert (c == s0 + s1 + s2 + s3[:-1] or
349            c == s0 + s1 + s3 + s2[:-1] or
350            c == s0 + s2 + s1 + s3[:-1] or
351            c == s0 + s2 + s3 + s1[:-1] or
352            c == s0 + s3 + s1 + s2[:-1] or
353            c == s0 + s3 + s2 + s1[:-1])
354
355
356def test_Matrix_printing():
357    # Test returning a Matrix
358
359    mat = Matrix([x*y, Piecewise((2 + x, y>0), (y, True)), sin(z)])
360    A = MatrixSymbol('A', 3, 1)
361    assert glsl_code(mat, assign_to=A) == (
362'''A[0][0] = x*y;
363if (y > 0) {
364   A[1][0] = x + 2;
365}
366else {
367   A[1][0] = y;
368}
369A[2][0] = sin(z);''' )
370    assert glsl_code(Matrix([A[0],A[1]]))
371    # Test using MatrixElements in expressions
372    expr = Piecewise((2*A[2, 0], x > 0), (A[2, 0], True)) + sin(A[1, 0]) + A[0, 0]
373    assert glsl_code(expr) == (
374'''((x > 0) ? (
375   2*A[2][0]
376)
377: (
378   A[2][0]
379)) + sin(A[1][0]) + A[0][0]''' )
380
381    # Test using MatrixElements in a Matrix
382    q = MatrixSymbol('q', 5, 1)
383    M = MatrixSymbol('M', 3, 3)
384    m = Matrix([[sin(q[1,0]), 0, cos(q[2,0])],
385        [q[1,0] + q[2,0], q[3, 0], 5],
386        [2*q[4, 0]/q[1,0], sqrt(q[0,0]) + 4, 0]])
387    assert glsl_code(m,M) == (
388'''M[0][0] = sin(q[1]);
389M[0][1] = 0;
390M[0][2] = cos(q[2]);
391M[1][0] = q[1] + q[2];
392M[1][1] = q[3];
393M[1][2] = 5;
394M[2][0] = 2*q[4]/q[1];
395M[2][1] = sqrt(q[0]) + 4;
396M[2][2] = 0;'''
397        )
398
399def test_Matrices_1x7():
400    gl = glsl_code
401    A = Matrix([1,2,3,4,5,6,7])
402    assert gl(A) == 'float[7](1, 2, 3, 4, 5, 6, 7)'
403    assert gl(A.transpose()) == 'float[7](1, 2, 3, 4, 5, 6, 7)'
404
405def test_Matrices_1x7_array_type_int():
406    gl = glsl_code
407    A = Matrix([1,2,3,4,5,6,7])
408    assert gl(A, array_type='int') == 'int[7](1, 2, 3, 4, 5, 6, 7)'
409
410def test_Tuple_array_type_custom():
411    gl = glsl_code
412    A = symbols('a b c')
413    assert gl(A, array_type='AbcType', glsl_types=False) == 'AbcType[3](a, b, c)'
414
415def test_Matrices_1x7_spread_assign_to_symbols():
416    gl = glsl_code
417    A = Matrix([1,2,3,4,5,6,7])
418    assign_to = symbols('x.a x.b x.c x.d x.e x.f x.g')
419    assert gl(A, assign_to=assign_to) == textwrap.dedent('''\
420        x.a = 1;
421        x.b = 2;
422        x.c = 3;
423        x.d = 4;
424        x.e = 5;
425        x.f = 6;
426        x.g = 7;'''
427    )
428
429def test_spread_assign_to_nested_symbols():
430    gl = glsl_code
431    expr = ((1,2,3), (1,2,3))
432    assign_to = (symbols('a b c'), symbols('x y z'))
433    assert gl(expr, assign_to=assign_to) == textwrap.dedent('''\
434        a = 1;
435        b = 2;
436        c = 3;
437        x = 1;
438        y = 2;
439        z = 3;'''
440    )
441
442def test_spread_assign_to_deeply_nested_symbols():
443    gl = glsl_code
444    a, b, c, x, y, z = symbols('a b c x y z')
445    expr = (((1,2),3), ((1,2),3))
446    assign_to = (((a, b), c), ((x, y), z))
447    assert gl(expr, assign_to=assign_to) == textwrap.dedent('''\
448        a = 1;
449        b = 2;
450        c = 3;
451        x = 1;
452        y = 2;
453        z = 3;'''
454    )
455
456def test_matrix_of_tuples_spread_assign_to_symbols():
457    gl = glsl_code
458    with warns_deprecated_sympy():
459        expr = Matrix([[(1,2),(3,4)],[(5,6),(7,8)]])
460    assign_to = (symbols('a b'), symbols('c d'), symbols('e f'), symbols('g h'))
461    assert gl(expr, assign_to) == textwrap.dedent('''\
462        a = 1;
463        b = 2;
464        c = 3;
465        d = 4;
466        e = 5;
467        f = 6;
468        g = 7;
469        h = 8;'''
470    )
471
472def test_cannot_assign_to_cause_mismatched_length():
473    expr = (1, 2)
474    assign_to = symbols('x y z')
475    raises(ValueError, lambda: glsl_code(expr, assign_to))
476
477def test_matrix_4x4_assign():
478    gl = glsl_code
479    expr = MatrixSymbol('A',4,4) * MatrixSymbol('B',4,4) + MatrixSymbol('C',4,4)
480    assign_to = MatrixSymbol('X',4,4)
481    assert gl(expr, assign_to=assign_to) == textwrap.dedent('''\
482        X[0][0] = A[0][0]*B[0][0] + A[0][1]*B[1][0] + A[0][2]*B[2][0] + A[0][3]*B[3][0] + C[0][0];
483        X[0][1] = A[0][0]*B[0][1] + A[0][1]*B[1][1] + A[0][2]*B[2][1] + A[0][3]*B[3][1] + C[0][1];
484        X[0][2] = A[0][0]*B[0][2] + A[0][1]*B[1][2] + A[0][2]*B[2][2] + A[0][3]*B[3][2] + C[0][2];
485        X[0][3] = A[0][0]*B[0][3] + A[0][1]*B[1][3] + A[0][2]*B[2][3] + A[0][3]*B[3][3] + C[0][3];
486        X[1][0] = A[1][0]*B[0][0] + A[1][1]*B[1][0] + A[1][2]*B[2][0] + A[1][3]*B[3][0] + C[1][0];
487        X[1][1] = A[1][0]*B[0][1] + A[1][1]*B[1][1] + A[1][2]*B[2][1] + A[1][3]*B[3][1] + C[1][1];
488        X[1][2] = A[1][0]*B[0][2] + A[1][1]*B[1][2] + A[1][2]*B[2][2] + A[1][3]*B[3][2] + C[1][2];
489        X[1][3] = A[1][0]*B[0][3] + A[1][1]*B[1][3] + A[1][2]*B[2][3] + A[1][3]*B[3][3] + C[1][3];
490        X[2][0] = A[2][0]*B[0][0] + A[2][1]*B[1][0] + A[2][2]*B[2][0] + A[2][3]*B[3][0] + C[2][0];
491        X[2][1] = A[2][0]*B[0][1] + A[2][1]*B[1][1] + A[2][2]*B[2][1] + A[2][3]*B[3][1] + C[2][1];
492        X[2][2] = A[2][0]*B[0][2] + A[2][1]*B[1][2] + A[2][2]*B[2][2] + A[2][3]*B[3][2] + C[2][2];
493        X[2][3] = A[2][0]*B[0][3] + A[2][1]*B[1][3] + A[2][2]*B[2][3] + A[2][3]*B[3][3] + C[2][3];
494        X[3][0] = A[3][0]*B[0][0] + A[3][1]*B[1][0] + A[3][2]*B[2][0] + A[3][3]*B[3][0] + C[3][0];
495        X[3][1] = A[3][0]*B[0][1] + A[3][1]*B[1][1] + A[3][2]*B[2][1] + A[3][3]*B[3][1] + C[3][1];
496        X[3][2] = A[3][0]*B[0][2] + A[3][1]*B[1][2] + A[3][2]*B[2][2] + A[3][3]*B[3][2] + C[3][2];
497        X[3][3] = A[3][0]*B[0][3] + A[3][1]*B[1][3] + A[3][2]*B[2][3] + A[3][3]*B[3][3] + C[3][3];'''
498    )
499
500def test_1xN_vecs():
501    gl = glsl_code
502    for i in range(1,10):
503        A = Matrix(range(i))
504        assert gl(A.transpose()) == gl(A)
505        assert gl(A,mat_transpose=True) == gl(A)
506        if i > 1:
507            if i <= 4:
508                assert gl(A) == 'vec%s(%s)' % (i,', '.join(str(s) for s in range(i)))
509            else:
510                assert gl(A) == 'float[%s](%s)' % (i,', '.join(str(s) for s in range(i)))
511
512def test_MxN_mats():
513    generatedAssertions='def test_misc_mats():\n'
514    for i in range(1,6):
515        for j in range(1,6):
516            A = Matrix([[x + y*j for x in range(j)] for y in range(i)])
517            gl = glsl_code(A)
518            glTransposed = glsl_code(A,mat_transpose=True)
519            generatedAssertions+='    mat = '+StrPrinter()._print(A)+'\n\n'
520            generatedAssertions+='    gl = \'\'\''+gl+'\'\'\'\n'
521            generatedAssertions+='    glTransposed = \'\'\''+glTransposed+'\'\'\'\n\n'
522            generatedAssertions+='    assert glsl_code(mat) == gl\n'
523            generatedAssertions+='    assert glsl_code(mat,mat_transpose=True) == glTransposed\n'
524            if i == 1 and j == 1:
525                assert gl == '0'
526            elif i <= 4 and j <= 4 and i>1 and j>1:
527                assert gl.startswith('mat%s' % j)
528                assert glTransposed.startswith('mat%s' % i)
529            elif i == 1 and j <= 4:
530                assert gl.startswith('vec')
531            elif j == 1 and i <= 4:
532                assert gl.startswith('vec')
533            elif i == 1:
534                assert gl.startswith('float[%s]('% j*i)
535                assert glTransposed.startswith('float[%s]('% j*i)
536            elif j == 1:
537                assert gl.startswith('float[%s]('% i*j)
538                assert glTransposed.startswith('float[%s]('% i*j)
539            else:
540                assert gl.startswith('float[%s](' % (i*j))
541                assert glTransposed.startswith('float[%s](' % (i*j))
542                glNested = glsl_code(A,mat_nested=True)
543                glNestedTransposed = glsl_code(A,mat_transpose=True,mat_nested=True)
544                assert glNested.startswith('float[%s][%s]' % (i,j))
545                assert glNestedTransposed.startswith('float[%s][%s]' % (j,i))
546                generatedAssertions+='    glNested = \'\'\''+glNested+'\'\'\'\n'
547                generatedAssertions+='    glNestedTransposed = \'\'\''+glNestedTransposed+'\'\'\'\n\n'
548                generatedAssertions+='    assert glsl_code(mat,mat_nested=True) == glNested\n'
549                generatedAssertions+='    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed\n\n'
550    generateAssertions = False # set this to true to write bake these generated tests to a file
551    if generateAssertions:
552        gen = open('test_glsl_generated_matrices.py','w')
553        gen.write(generatedAssertions)
554        gen.close()
555
556
557# these assertions were generated from the previous function
558# glsl has complicated rules and this makes it easier to look over all the cases
559def test_misc_mats():
560
561    mat = Matrix([[0]])
562
563    gl = '''0'''
564    glTransposed = '''0'''
565
566    assert glsl_code(mat) == gl
567    assert glsl_code(mat,mat_transpose=True) == glTransposed
568
569    mat = Matrix([[0, 1]])
570
571    gl = '''vec2(0, 1)'''
572    glTransposed = '''vec2(0, 1)'''
573
574    assert glsl_code(mat) == gl
575    assert glsl_code(mat,mat_transpose=True) == glTransposed
576
577    mat = Matrix([[0, 1, 2]])
578
579    gl = '''vec3(0, 1, 2)'''
580    glTransposed = '''vec3(0, 1, 2)'''
581
582    assert glsl_code(mat) == gl
583    assert glsl_code(mat,mat_transpose=True) == glTransposed
584
585    mat = Matrix([[0, 1, 2, 3]])
586
587    gl = '''vec4(0, 1, 2, 3)'''
588    glTransposed = '''vec4(0, 1, 2, 3)'''
589
590    assert glsl_code(mat) == gl
591    assert glsl_code(mat,mat_transpose=True) == glTransposed
592
593    mat = Matrix([[0, 1, 2, 3, 4]])
594
595    gl = '''float[5](0, 1, 2, 3, 4)'''
596    glTransposed = '''float[5](0, 1, 2, 3, 4)'''
597
598    assert glsl_code(mat) == gl
599    assert glsl_code(mat,mat_transpose=True) == glTransposed
600
601    mat = Matrix([
602[0],
603[1]])
604
605    gl = '''vec2(0, 1)'''
606    glTransposed = '''vec2(0, 1)'''
607
608    assert glsl_code(mat) == gl
609    assert glsl_code(mat,mat_transpose=True) == glTransposed
610
611    mat = Matrix([
612[0, 1],
613[2, 3]])
614
615    gl = '''mat2(0, 1, 2, 3)'''
616    glTransposed = '''mat2(0, 2, 1, 3)'''
617
618    assert glsl_code(mat) == gl
619    assert glsl_code(mat,mat_transpose=True) == glTransposed
620
621    mat = Matrix([
622[0, 1, 2],
623[3, 4, 5]])
624
625    gl = '''mat3x2(0, 1, 2, 3, 4, 5)'''
626    glTransposed = '''mat2x3(0, 3, 1, 4, 2, 5)'''
627
628    assert glsl_code(mat) == gl
629    assert glsl_code(mat,mat_transpose=True) == glTransposed
630
631    mat = Matrix([
632[0, 1, 2, 3],
633[4, 5, 6, 7]])
634
635    gl = '''mat4x2(0, 1, 2, 3, 4, 5, 6, 7)'''
636    glTransposed = '''mat2x4(0, 4, 1, 5, 2, 6, 3, 7)'''
637
638    assert glsl_code(mat) == gl
639    assert glsl_code(mat,mat_transpose=True) == glTransposed
640
641    mat = Matrix([
642[0, 1, 2, 3, 4],
643[5, 6, 7, 8, 9]])
644
645    gl = '''float[10](
646   0, 1, 2, 3, 4,
647   5, 6, 7, 8, 9
648) /* a 2x5 matrix */'''
649    glTransposed = '''float[10](
650   0, 5,
651   1, 6,
652   2, 7,
653   3, 8,
654   4, 9
655) /* a 5x2 matrix */'''
656
657    assert glsl_code(mat) == gl
658    assert glsl_code(mat,mat_transpose=True) == glTransposed
659    glNested = '''float[2][5](
660   float[](0, 1, 2, 3, 4),
661   float[](5, 6, 7, 8, 9)
662)'''
663    glNestedTransposed = '''float[5][2](
664   float[](0, 5),
665   float[](1, 6),
666   float[](2, 7),
667   float[](3, 8),
668   float[](4, 9)
669)'''
670
671    assert glsl_code(mat,mat_nested=True) == glNested
672    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed
673
674    mat = Matrix([
675[0],
676[1],
677[2]])
678
679    gl = '''vec3(0, 1, 2)'''
680    glTransposed = '''vec3(0, 1, 2)'''
681
682    assert glsl_code(mat) == gl
683    assert glsl_code(mat,mat_transpose=True) == glTransposed
684
685    mat = Matrix([
686[0, 1],
687[2, 3],
688[4, 5]])
689
690    gl = '''mat2x3(0, 1, 2, 3, 4, 5)'''
691    glTransposed = '''mat3x2(0, 2, 4, 1, 3, 5)'''
692
693    assert glsl_code(mat) == gl
694    assert glsl_code(mat,mat_transpose=True) == glTransposed
695
696    mat = Matrix([
697[0, 1, 2],
698[3, 4, 5],
699[6, 7, 8]])
700
701    gl = '''mat3(0, 1, 2, 3, 4, 5, 6, 7, 8)'''
702    glTransposed = '''mat3(0, 3, 6, 1, 4, 7, 2, 5, 8)'''
703
704    assert glsl_code(mat) == gl
705    assert glsl_code(mat,mat_transpose=True) == glTransposed
706
707    mat = Matrix([
708[0, 1,  2,  3],
709[4, 5,  6,  7],
710[8, 9, 10, 11]])
711
712    gl = '''mat4x3(0, 1,  2,  3, 4, 5,  6,  7, 8, 9, 10, 11)'''
713    glTransposed = '''mat3x4(0, 4,  8, 1, 5,  9, 2, 6, 10, 3, 7, 11)'''
714
715    assert glsl_code(mat) == gl
716    assert glsl_code(mat,mat_transpose=True) == glTransposed
717
718    mat = Matrix([
719[ 0,  1,  2,  3,  4],
720[ 5,  6,  7,  8,  9],
721[10, 11, 12, 13, 14]])
722
723    gl = '''float[15](
724   0,  1,  2,  3,  4,
725   5,  6,  7,  8,  9,
726   10, 11, 12, 13, 14
727) /* a 3x5 matrix */'''
728    glTransposed = '''float[15](
729   0, 5, 10,
730   1, 6, 11,
731   2, 7, 12,
732   3, 8, 13,
733   4, 9, 14
734) /* a 5x3 matrix */'''
735
736    assert glsl_code(mat) == gl
737    assert glsl_code(mat,mat_transpose=True) == glTransposed
738    glNested = '''float[3][5](
739   float[]( 0,  1,  2,  3,  4),
740   float[]( 5,  6,  7,  8,  9),
741   float[](10, 11, 12, 13, 14)
742)'''
743    glNestedTransposed = '''float[5][3](
744   float[](0, 5, 10),
745   float[](1, 6, 11),
746   float[](2, 7, 12),
747   float[](3, 8, 13),
748   float[](4, 9, 14)
749)'''
750
751    assert glsl_code(mat,mat_nested=True) == glNested
752    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed
753
754    mat = Matrix([
755[0],
756[1],
757[2],
758[3]])
759
760    gl = '''vec4(0, 1, 2, 3)'''
761    glTransposed = '''vec4(0, 1, 2, 3)'''
762
763    assert glsl_code(mat) == gl
764    assert glsl_code(mat,mat_transpose=True) == glTransposed
765
766    mat = Matrix([
767[0, 1],
768[2, 3],
769[4, 5],
770[6, 7]])
771
772    gl = '''mat2x4(0, 1, 2, 3, 4, 5, 6, 7)'''
773    glTransposed = '''mat4x2(0, 2, 4, 6, 1, 3, 5, 7)'''
774
775    assert glsl_code(mat) == gl
776    assert glsl_code(mat,mat_transpose=True) == glTransposed
777
778    mat = Matrix([
779[0,  1,  2],
780[3,  4,  5],
781[6,  7,  8],
782[9, 10, 11]])
783
784    gl = '''mat3x4(0,  1,  2, 3,  4,  5, 6,  7,  8, 9, 10, 11)'''
785    glTransposed = '''mat4x3(0, 3, 6,  9, 1, 4, 7, 10, 2, 5, 8, 11)'''
786
787    assert glsl_code(mat) == gl
788    assert glsl_code(mat,mat_transpose=True) == glTransposed
789
790    mat = Matrix([
791[ 0,  1,  2,  3],
792[ 4,  5,  6,  7],
793[ 8,  9, 10, 11],
794[12, 13, 14, 15]])
795
796    gl = '''mat4( 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15)'''
797    glTransposed = '''mat4(0, 4,  8, 12, 1, 5,  9, 13, 2, 6, 10, 14, 3, 7, 11, 15)'''
798
799    assert glsl_code(mat) == gl
800    assert glsl_code(mat,mat_transpose=True) == glTransposed
801
802    mat = Matrix([
803[ 0,  1,  2,  3,  4],
804[ 5,  6,  7,  8,  9],
805[10, 11, 12, 13, 14],
806[15, 16, 17, 18, 19]])
807
808    gl = '''float[20](
809   0,  1,  2,  3,  4,
810   5,  6,  7,  8,  9,
811   10, 11, 12, 13, 14,
812   15, 16, 17, 18, 19
813) /* a 4x5 matrix */'''
814    glTransposed = '''float[20](
815   0, 5, 10, 15,
816   1, 6, 11, 16,
817   2, 7, 12, 17,
818   3, 8, 13, 18,
819   4, 9, 14, 19
820) /* a 5x4 matrix */'''
821
822    assert glsl_code(mat) == gl
823    assert glsl_code(mat,mat_transpose=True) == glTransposed
824    glNested = '''float[4][5](
825   float[]( 0,  1,  2,  3,  4),
826   float[]( 5,  6,  7,  8,  9),
827   float[](10, 11, 12, 13, 14),
828   float[](15, 16, 17, 18, 19)
829)'''
830    glNestedTransposed = '''float[5][4](
831   float[](0, 5, 10, 15),
832   float[](1, 6, 11, 16),
833   float[](2, 7, 12, 17),
834   float[](3, 8, 13, 18),
835   float[](4, 9, 14, 19)
836)'''
837
838    assert glsl_code(mat,mat_nested=True) == glNested
839    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed
840
841    mat = Matrix([
842[0],
843[1],
844[2],
845[3],
846[4]])
847
848    gl = '''float[5](0, 1, 2, 3, 4)'''
849    glTransposed = '''float[5](0, 1, 2, 3, 4)'''
850
851    assert glsl_code(mat) == gl
852    assert glsl_code(mat,mat_transpose=True) == glTransposed
853
854    mat = Matrix([
855[0, 1],
856[2, 3],
857[4, 5],
858[6, 7],
859[8, 9]])
860
861    gl = '''float[10](
862   0, 1,
863   2, 3,
864   4, 5,
865   6, 7,
866   8, 9
867) /* a 5x2 matrix */'''
868    glTransposed = '''float[10](
869   0, 2, 4, 6, 8,
870   1, 3, 5, 7, 9
871) /* a 2x5 matrix */'''
872
873    assert glsl_code(mat) == gl
874    assert glsl_code(mat,mat_transpose=True) == glTransposed
875    glNested = '''float[5][2](
876   float[](0, 1),
877   float[](2, 3),
878   float[](4, 5),
879   float[](6, 7),
880   float[](8, 9)
881)'''
882    glNestedTransposed = '''float[2][5](
883   float[](0, 2, 4, 6, 8),
884   float[](1, 3, 5, 7, 9)
885)'''
886
887    assert glsl_code(mat,mat_nested=True) == glNested
888    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed
889
890    mat = Matrix([
891[ 0,  1,  2],
892[ 3,  4,  5],
893[ 6,  7,  8],
894[ 9, 10, 11],
895[12, 13, 14]])
896
897    gl = '''float[15](
898   0,  1,  2,
899   3,  4,  5,
900   6,  7,  8,
901   9, 10, 11,
902   12, 13, 14
903) /* a 5x3 matrix */'''
904    glTransposed = '''float[15](
905   0, 3, 6,  9, 12,
906   1, 4, 7, 10, 13,
907   2, 5, 8, 11, 14
908) /* a 3x5 matrix */'''
909
910    assert glsl_code(mat) == gl
911    assert glsl_code(mat,mat_transpose=True) == glTransposed
912    glNested = '''float[5][3](
913   float[]( 0,  1,  2),
914   float[]( 3,  4,  5),
915   float[]( 6,  7,  8),
916   float[]( 9, 10, 11),
917   float[](12, 13, 14)
918)'''
919    glNestedTransposed = '''float[3][5](
920   float[](0, 3, 6,  9, 12),
921   float[](1, 4, 7, 10, 13),
922   float[](2, 5, 8, 11, 14)
923)'''
924
925    assert glsl_code(mat,mat_nested=True) == glNested
926    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed
927
928    mat = Matrix([
929[ 0,  1,  2,  3],
930[ 4,  5,  6,  7],
931[ 8,  9, 10, 11],
932[12, 13, 14, 15],
933[16, 17, 18, 19]])
934
935    gl = '''float[20](
936   0,  1,  2,  3,
937   4,  5,  6,  7,
938   8,  9, 10, 11,
939   12, 13, 14, 15,
940   16, 17, 18, 19
941) /* a 5x4 matrix */'''
942    glTransposed = '''float[20](
943   0, 4,  8, 12, 16,
944   1, 5,  9, 13, 17,
945   2, 6, 10, 14, 18,
946   3, 7, 11, 15, 19
947) /* a 4x5 matrix */'''
948
949    assert glsl_code(mat) == gl
950    assert glsl_code(mat,mat_transpose=True) == glTransposed
951    glNested = '''float[5][4](
952   float[]( 0,  1,  2,  3),
953   float[]( 4,  5,  6,  7),
954   float[]( 8,  9, 10, 11),
955   float[](12, 13, 14, 15),
956   float[](16, 17, 18, 19)
957)'''
958    glNestedTransposed = '''float[4][5](
959   float[](0, 4,  8, 12, 16),
960   float[](1, 5,  9, 13, 17),
961   float[](2, 6, 10, 14, 18),
962   float[](3, 7, 11, 15, 19)
963)'''
964
965    assert glsl_code(mat,mat_nested=True) == glNested
966    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed
967
968    mat = Matrix([
969[ 0,  1,  2,  3,  4],
970[ 5,  6,  7,  8,  9],
971[10, 11, 12, 13, 14],
972[15, 16, 17, 18, 19],
973[20, 21, 22, 23, 24]])
974
975    gl = '''float[25](
976   0,  1,  2,  3,  4,
977   5,  6,  7,  8,  9,
978   10, 11, 12, 13, 14,
979   15, 16, 17, 18, 19,
980   20, 21, 22, 23, 24
981) /* a 5x5 matrix */'''
982    glTransposed = '''float[25](
983   0, 5, 10, 15, 20,
984   1, 6, 11, 16, 21,
985   2, 7, 12, 17, 22,
986   3, 8, 13, 18, 23,
987   4, 9, 14, 19, 24
988) /* a 5x5 matrix */'''
989
990    assert glsl_code(mat) == gl
991    assert glsl_code(mat,mat_transpose=True) == glTransposed
992    glNested = '''float[5][5](
993   float[]( 0,  1,  2,  3,  4),
994   float[]( 5,  6,  7,  8,  9),
995   float[](10, 11, 12, 13, 14),
996   float[](15, 16, 17, 18, 19),
997   float[](20, 21, 22, 23, 24)
998)'''
999    glNestedTransposed = '''float[5][5](
1000   float[](0, 5, 10, 15, 20),
1001   float[](1, 6, 11, 16, 21),
1002   float[](2, 7, 12, 17, 22),
1003   float[](3, 8, 13, 18, 23),
1004   float[](4, 9, 14, 19, 24)
1005)'''
1006
1007    assert glsl_code(mat,mat_nested=True) == glNested
1008    assert glsl_code(mat,mat_nested=True,mat_transpose=True) == glNestedTransposed
1009