3

I need to calculate the inverse of a 4x4 matrix in a JavaScript application. This goes well beyond my math capabilities, and doing some "research" (read: Googled it) I found a Stack Overflow answer which works perfectly.

The poster mentions that this particular implementation is from the MESA implementation of GLU, which can be found here.

The file containing the function starts with the following license notice:

/*
* SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
* Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice including the dates of first publication and
* either this permission notice or a reference to
* http://oss.sgi.com/projects/FreeB/
* shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of Silicon Graphics, Inc.
* shall not be used in advertising or otherwise to promote the sale, use or
* other dealings in this Software without prior written authorization from
* Silicon Graphics, Inc.
*/

My understanding of this license is that I'd need to include this license both with any source and "binary" (it is JavaScript after all) distributions, and in theory, the mentioned Stack Overflow answer should also include it. Is this correct, or am I misinterpreting its requirements?

I've additionally made small changes to it, to make it parse as JavaScript and to remove a temporary variable. Would I need to identify these changes?

function InvertMatrix(from, to) {
    /* This is based on the MESA GLU source, as originally seen at https://stackoverflow.com/a/1148405. */
    /*
    * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
    * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
    *
    * Permission is hereby granted, free of charge, to any person obtaining a
    * copy of this software and associated documentation files (the "Software"),
    * to deal in the Software without restriction, including without limitation
    * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    * and/or sell copies of the Software, and to permit persons to whom the
    * Software is furnished to do so, subject to the following conditions:
    *
    * The above copyright notice including the dates of first publication and
    * either this permission notice or a reference to
    * http://oss.sgi.com/projects/FreeB/
    * shall be included in all copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
    * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    * SOFTWARE.
    *
    * Except as contained in this notice, the name of Silicon Graphics, Inc.
    * shall not be used in advertising or otherwise to promote the sale, use or
    * other dealings in this Software without prior written authorization from
    * Silicon Graphics, Inc.
    */

    to[0] = from[5] * from[10] * from[15] -
        from[5] * from[11] * from[14] -
        from[9] * from[6] * from[15] +
        from[9] * from[7] * from[14] +
        from[13] * from[6] * from[11] -
        from[13] * from[7] * from[10];

    to[4] = -from[4] * from[10] * from[15] +
        from[4] * from[11] * from[14] +
        from[8] * from[6] * from[15] -
        from[8] * from[7] * from[14] -
        from[12] * from[6] * from[11] +
        from[12] * from[7] * from[10];

    to[8] = from[4] * from[9] * from[15] -
        from[4] * from[11] * from[13] -
        from[8] * from[5] * from[15] +
        from[8] * from[7] * from[13] +
        from[12] * from[5] * from[11] -
        from[12] * from[7] * from[9];

    to[12] = -from[4] * from[9] * from[14] +
        from[4] * from[10] * from[13] +
        from[8] * from[5] * from[14] -
        from[8] * from[6] * from[13] -
        from[12] * from[5] * from[10] +
        from[12] * from[6] * from[9];

    to[1] = -from[1] * from[10] * from[15] +
        from[1] * from[11] * from[14] +
        from[9] * from[2] * from[15] -
        from[9] * from[3] * from[14] -
        from[13] * from[2] * from[11] +
        from[13] * from[3] * from[10];

    to[5] = from[0] * from[10] * from[15] -
        from[0] * from[11] * from[14] -
        from[8] * from[2] * from[15] +
        from[8] * from[3] * from[14] +
        from[12] * from[2] * from[11] -
        from[12] * from[3] * from[10];

    to[9] = -from[0] * from[9] * from[15] +
        from[0] * from[11] * from[13] +
        from[8] * from[1] * from[15] -
        from[8] * from[3] * from[13] -
        from[12] * from[1] * from[11] +
        from[12] * from[3] * from[9];

    to[13] = from[0] * from[9] * from[14] -
        from[0] * from[10] * from[13] -
        from[8] * from[1] * from[14] +
        from[8] * from[2] * from[13] +
        from[12] * from[1] * from[10] -
        from[12] * from[2] * from[9];

    to[2] = from[1] * from[6] * from[15] -
        from[1] * from[7] * from[14] -
        from[5] * from[2] * from[15] +
        from[5] * from[3] * from[14] +
        from[13] * from[2] * from[7] -
        from[13] * from[3] * from[6];

    to[6] = -from[0] * from[6] * from[15] +
        from[0] * from[7] * from[14] +
        from[4] * from[2] * from[15] -
        from[4] * from[3] * from[14] -
        from[12] * from[2] * from[7] +
        from[12] * from[3] * from[6];

    to[10] = from[0] * from[5] * from[15] -
        from[0] * from[7] * from[13] -
        from[4] * from[1] * from[15] +
        from[4] * from[3] * from[13] +
        from[12] * from[1] * from[7] -
        from[12] * from[3] * from[5];

    to[14] = -from[0] * from[5] * from[14] +
        from[0] * from[6] * from[13] +
        from[4] * from[1] * from[14] -
        from[4] * from[2] * from[13] -
        from[12] * from[1] * from[6] +
        from[12] * from[2] * from[5];

    to[3] = -from[1] * from[6] * from[11] +
        from[1] * from[7] * from[10] +
        from[5] * from[2] * from[11] -
        from[5] * from[3] * from[10] -
        from[9] * from[2] * from[7] +
        from[9] * from[3] * from[6];

    to[7] = from[0] * from[6] * from[11] -
        from[0] * from[7] * from[10] -
        from[4] * from[2] * from[11] +
        from[4] * from[3] * from[10] +
        from[8] * from[2] * from[7] -
        from[8] * from[3] * from[6];

    to[11] = -from[0] * from[5] * from[11] +
        from[0] * from[7] * from[9] +
        from[4] * from[1] * from[11] -
        from[4] * from[3] * from[9] -
        from[8] * from[1] * from[7] +
        from[8] * from[3] * from[5];

    to[15] = from[0] * from[5] * from[10] -
        from[0] * from[6] * from[9] -
        from[4] * from[1] * from[10] +
        from[4] * from[2] * from[9] +
        from[8] * from[1] * from[6] -
        from[8] * from[2] * from[5];

    var determinant = from[0] * to[0] + from[1] * to[4] + from[2] * to[8] + from[3] * to[12];

    if (!determinant) throw "Failed to invert a matrix"

    determinant = 1.0 / determinant;

    for (i = 0; i < 16; i++)
        to[i] *= determinant;
}

(moved from https://codereview.stackexchange.com/posts/180218 by request)

  • That code might be considered too trivial to be copyrightable. But IANAL. – curiousdannii Nov 12 '17 at 13:50
  • For the copyrightability question, try changing the variable names, use 2-dimensional indices and then and see if you still consider it original. E.g. b[0][0] = a[1][1] * a[2][2] * a[3][3] - a[1][1] * a[2][3] * a[3][2] - .... For non-trivial code, making trivial changes like changing variable names or indexing does not destroy its originality. – Brandin Nov 13 '17 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.