Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /trunk/src/include/Diderot/inline-matrix.h
ViewVC logotype

View of /trunk/src/include/Diderot/inline-matrix.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2356 - (download) (as text) (annotate)
Sun Apr 7 14:45:25 2013 UTC (6 years, 4 months ago) by jhr
File size: 8037 byte(s)
  Merging in bug fixes and language enhancements from the vis12 branch (via staging).
  Features include type promotion, the curl and colon operator, transpose, and functions.
/*! \file inline-matrix.h
 *
 * \author John Reppy
 */

/*
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#ifndef _DIDEROT_INLINE_MATRIX_H_
#define _DIDEROT_INLINE_MATRIX_H_

#ifndef _DIDEROT_TYPES_H_
#include "types.h"
#endif
#ifndef _DIDEROT_INLINE_VEC2_H_
#  include "inline-vec2.h"
#endif
#ifndef _DIDEROT_INLINE_VEC3_H_
#  include "inline-vec3.h"
#endif
#ifndef _DIDEROT_INLINE_VEC4_H_
#  include "inline-vec4.h"
#endif


/********** 2x2 matrix functions **********/

STATIC_INLINE void zero2x2 (Diderot_Mat2x2_t dst)
{
    dst[0].v = vec2(0.0, 0.0);
    dst[1].v = vec2(0.0, 0.0);
}

STATIC_INLINE void identity2x2 (Diderot_Mat2x2_t dst)
{
    dst[0].v = vec2(1.0, 0.0);
    dst[1].v = vec2(0.0, 1.0);
}

STATIC_INLINE Diderot_vec2_t column2x2 (Diderot_Mat2x2_t m, int i)
{
    return vec2(m[0].r[i], m[1].r[i]);
}

STATIC_INLINE void copy2x2 (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t src)
{
    dst[0].v = src[0].v;
    dst[1].v = src[1].v;
}

STATIC_INLINE void scale2x2 (Diderot_Mat2x2_t dst, Diderot_real_t s, Diderot_Mat2x2_t src)
{
    Diderot_vec2_t scale = vec2(s, s);
    dst[0].v = scale * src[0].v;
    dst[1].v = scale * src[1].v;
}

STATIC_INLINE void add2x2 (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t a, Diderot_Mat2x2_t b)
{
    dst[0].v = a[0].v + b[0].v;
    dst[1].v = a[1].v + b[1].v;
}

STATIC_INLINE void sub2x2 (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t a, Diderot_Mat2x2_t b)
{
    dst[0].v = a[0].v - b[0].v;
    dst[1].v = a[1].v - b[1].v;
}

STATIC_INLINE Diderot_vec2_t mulVec2Mat2x2 (Diderot_vec2_t v, Diderot_Mat2x2_t m)
{
    return vec2(
	dot2(v, column2x2(m, 0)),
	dot2(v, column2x2(m, 1)));
}

STATIC_INLINE Diderot_vec2_t mulMat2x2Vec2 (Diderot_Mat2x2_t m, Diderot_vec2_t v)
{
    return vec2(dot2(m[0].v, v), dot2(m[1].v, v));
}

STATIC_INLINE void mulMat2x2Mat2x2 (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t m1, Diderot_Mat2x2_t m2)
{
    dst[0].v = vec2(
	dot2(m1[0].v, column2x2(m2, 0)),
	dot2(m1[0].v, column2x2(m2, 1)));
    dst[1].v = vec2(
	dot2(m1[1].v, column2x2(m2, 0)),
	dot2(m1[1].v, column2x2(m2, 1)));
}

STATIC_INLINE void transpose2x2 (Diderot_Mat2x2_t dst, Diderot_Mat2x2_t src)
{
    dst[0].v = column2x2(src, 0);
    dst[1].v = column2x2(src, 1);
}

// The Frobenius norm of a matrix is the sqrt of the sum of the squares of the elements
STATIC_INLINE Diderot_real_t norm2x2 (Diderot_Mat2x2_t m)
{
    return SQRT(dot2(m[0].v,m[0].v) + dot2(m[1].v,m[1].v));
}

// matrix time matrix ":" product
STATIC_INLINE Diderot_real_t colonProdMat2x2Mat2x2 (Diderot_Mat2x2_t m1, Diderot_Mat2x2_t m2)
{
    return dot2(m1[0].v, m2[0].v) + dot2(m1[1].v, m2[1].v);
}

/********** 3x3 matrix functions **********/

STATIC_INLINE void zero3x3 (Diderot_Mat3x3_t dst)
{
    dst[0].v = vec3(0.0, 0.0, 0.0);
    dst[1].v = vec3(0.0, 0.0, 0.0);
    dst[2].v = vec3(0.0, 0.0, 0.0);
}

STATIC_INLINE void identity3x3 (Diderot_Mat3x3_t dst)
{
    dst[0].v = vec3(1.0, 0.0, 0.0);
    dst[1].v = vec3(0.0, 1.0, 0.0);
    dst[2].v = vec3(0.0, 0.0, 1.0);
}

STATIC_INLINE Diderot_vec3_t column3x3 (Diderot_Mat3x3_t m, int i)
{
    return vec3(m[0].r[i], m[1].r[i], m[2].r[i]);
}

STATIC_INLINE void copy3x3 (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t src)
{
    dst[0].v = src[0].v;
    dst[1].v = src[1].v;
    dst[2].v = src[2].v;
}

STATIC_INLINE void scale3x3 (Diderot_Mat3x3_t dst, Diderot_real_t s, Diderot_Mat3x3_t src)
{
    Diderot_vec3_t scale = vec3(s, s, s);
    dst[0].v = scale * src[0].v;
    dst[1].v = scale * src[1].v;
    dst[2].v = scale * src[2].v;
}

STATIC_INLINE void add3x3 (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t a, Diderot_Mat3x3_t b)
{
    dst[0].v = a[0].v + b[0].v;
    dst[1].v = a[1].v + b[1].v;
    dst[2].v = a[2].v + b[2].v;
}

STATIC_INLINE void sub3x3 (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t a, Diderot_Mat3x3_t b)
{
    dst[0].v = a[0].v - b[0].v;
    dst[1].v = a[1].v - b[1].v;
    dst[2].v = a[2].v - b[2].v;
}

STATIC_INLINE Diderot_vec3_t mulVec3Mat3x3 (Diderot_vec3_t v, Diderot_Mat3x3_t m)
{
    return vec3(
	dot3(v, column3x3(m, 0)),
	dot3(v, column3x3(m, 1)),
	dot3(v, column3x3(m, 2)));
}

STATIC_INLINE Diderot_vec3_t mulMat3x3Vec3 (Diderot_Mat3x3_t m, Diderot_vec3_t v)
{
    return vec3(dot3(m[0].v, v), dot3(m[1].v, v), dot3(m[2].v, v));
}

STATIC_INLINE void mulMat3x3Mat3x3 (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t m1, Diderot_Mat3x3_t m2)
{
    dst[0].v = vec3(
	dot3(m1[0].v, column3x3(m2, 0)),
	dot3(m1[0].v, column3x3(m2, 1)),
	dot3(m1[0].v, column3x3(m2, 2)));
    dst[1].v = vec3(
	dot3(m1[1].v, column3x3(m2, 0)),
	dot3(m1[1].v, column3x3(m2, 1)),
	dot3(m1[1].v, column3x3(m2, 2)));
    dst[2].v = vec3(
	dot3(m1[2].v, column3x3(m2, 0)),
	dot3(m1[2].v, column3x3(m2, 1)),
	dot3(m1[2].v, column3x3(m2, 2)));
}

STATIC_INLINE void transpose3x3 (Diderot_Mat3x3_t dst, Diderot_Mat3x3_t src)
{
    dst[0].v = column3x3(src, 0);
    dst[1].v = column3x3(src, 1);
    dst[2].v = column3x3(src, 2);
}

// The Frobenius norm of a matrix is the sqrt of the sum of the squares of the elements
STATIC_INLINE Diderot_real_t norm3x3 (Diderot_Mat3x3_t m)
{
    return SQRT(dot3(m[0].v,m[0].v) + dot3(m[1].v,m[1].v) + dot3(m[2].v,m[2].v));
}

// matrix time matrix ":" product
STATIC_INLINE Diderot_real_t colonProdMat3x3Mat3x3 (Diderot_Mat3x3_t m1, Diderot_Mat3x3_t m2)
{
    return dot3(m1[0].v,m2[0].v) + dot3(m1[1].v,m2[1].v) + dot3(m1[2].v,m2[2].v);
}


/********** 4x4 matrix functions **********/

STATIC_INLINE void zero4x4 (Diderot_Mat4x4_t dst)
{
    dst[0].v = vec4(0.0, 0.0, 0.0, 0.0);
    dst[1].v = vec4(0.0, 0.0, 0.0, 0.0);
    dst[2].v = vec4(0.0, 0.0, 0.0, 0.0);
    dst[3].v = vec4(0.0, 0.0, 0.0, 0.0);
}

STATIC_INLINE void identity4x4 (Diderot_Mat4x4_t dst)
{
    dst[0].v = vec4(1.0, 0.0, 0.0, 0.0);
    dst[1].v = vec4(0.0, 1.0, 0.0, 0.0);
    dst[2].v = vec4(0.0, 0.0, 1.0, 0.0);
    dst[3].v = vec4(0.0, 0.0, 0.0, 1.0);
}

STATIC_INLINE Diderot_vec4_t column4x4 (Diderot_Mat4x4_t m, int i)
{
    return vec4(m[0].r[i], m[1].r[i], m[2].r[i], m[3].r[i]);
}

STATIC_INLINE void copy4x4 (Diderot_Mat4x4_t dst, Diderot_Mat4x4_t src)
{
    dst[0].v = src[0].v;
    dst[1].v = src[1].v;
    dst[2].v = src[2].v;
    dst[3].v = src[3].v;
}

STATIC_INLINE void scale4x4 (Diderot_Mat4x4_t dst, Diderot_real_t s, Diderot_Mat4x4_t src)
{
    Diderot_vec4_t scale = vec4(s, s, s, s);
    dst[0].v = scale * src[0].v;
    dst[1].v = scale * src[1].v;
    dst[2].v = scale * src[2].v;
    dst[3].v = scale * src[3].v;
}

STATIC_INLINE void add4x4 (Diderot_Mat4x4_t dst, Diderot_Mat4x4_t a, Diderot_Mat4x4_t b)
{
    dst[0].v = a[0].v + b[0].v;
    dst[1].v = a[1].v + b[1].v;
    dst[2].v = a[2].v + b[2].v;
    dst[3].v = a[3].v + b[3].v;
}

STATIC_INLINE void sub4x4 (Diderot_Mat4x4_t dst, Diderot_Mat4x4_t a, Diderot_Mat4x4_t b)
{
    dst[0].v = a[0].v - b[0].v;
    dst[1].v = a[1].v - b[1].v;
    dst[2].v = a[2].v - b[2].v;
    dst[3].v = a[3].v - b[3].v;
}

STATIC_INLINE Diderot_vec4_t mulVec4Mat4x4 (Diderot_vec4_t v, Diderot_Mat4x4_t m)
{
    return vec4(
	dot4(v, column4x4(m, 0)),
	dot4(v, column4x4(m, 1)),
	dot4(v, column4x4(m, 2)),
	dot4(v, column4x4(m,3)));
}

STATIC_INLINE Diderot_vec4_t mulMat4x4Vec4 (Diderot_Mat4x4_t m, Diderot_vec4_t v)
{
    return vec4(dot4(m[0].v, v), dot4(m[1].v, v), dot4(m[2].v, v), dot4(m[3].v, v));
}

STATIC_INLINE void transpose4x4 (Diderot_Mat4x4_t dst, Diderot_Mat4x4_t src)
{
    dst[0].v = column4x4(src, 0);
    dst[1].v = column4x4(src, 1);
    dst[2].v = column4x4(src, 2);
    dst[3].v = column4x4(src, 3);
}

// The Frobenius norm of a matrix is the sqrt of the sum of the squares of the elements
STATIC_INLINE Diderot_real_t norm4x4 (Diderot_Mat4x4_t m)
{
    return sqrt(dot4(m[0].v,m[0].v) + dot4(m[1].v,m[1].v) + dot4(m[2].v,m[2].v) + dot4(m[3].v,m[3].v));
}

// matrix time matrix ":" product
STATIC_INLINE Diderot_real_t colonProdMat4x4Mat4x4 (Diderot_Mat4x4_t m1, Diderot_Mat4x4_t m2)
{
    return dot4(m1[0].v,m2[0].v) + dot4(m1[1].v,m2[1].v) + dot4(m1[2].v,m2[2].v) + dot4(m1[3].v,m2[3].v);
}

#endif /* !_DIDEROT_INLINE_MATRIX_H_ */

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0