Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6226237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:59:16+00:00 2026-05-24T08:59:16+00:00

So I am trying to model Gram-Schmidt for any size N × N matrix,

  • 0

So I am trying to model Gram-Schmidt for any size N×N matrix, and I have officially hit a roadblock I can’t get past. I know it’s a matter of looping this correctly, but I can’t figure out what the problem is. Remember I do not want to just pass in a 3×3 matrix, but any size N×N.

The course notes QR Decomposition with Gram-Schmidt explains exactly what I want to do. Very simple calculation by the way. In the course notes ||u|| means that it is the sum of the square of the elements, so sqrt(x12 + x22 + x32 + …. + xn2).

The multiplication symbol is actually the dot product.

The code I wrote so far is listed below. What is wrong with it?

function qrProjection(arr) {
    var qProjected = [];
    var tempArray = [];
    var aTemp = arr;
    var uTemp = new Array(arr.length);
    var uSquareSqrt = new Array(arr.length);
    var eTemp = [];
    var sum = 0;
    var sumOfSquares = 0;
    var breakCondition = 0;
    var secondBreakCondition = 0;
    var iterationCounter = 0;

    //Build uTemp Array
    for (i = 0; i < arr.length; i++) {
        uTemp[i] = new Array(arr[i].length);
    }
    for (i = 0; i < arr.length; i++) {
        eTemp[i] = new Array(arr[i].length);
    }

    uTemp[0] = aTemp[0];

    for (j = 0; j <= arr.length; j++) {

        for (l = 0; l < arr[j].length; l++) {
            if (breakCondition == 1) break;
            sumOfSquares = Math.pow(uTemp[j][l], 2) + sumOfSquares;
        }

        if (breakCondition == 0) {
            uSquareSqrt[j] = Math.sqrt(sumOfSquares);
            sumOfSquares = 0;
        }

        for (i = 0; i < arr[j].length; i++) {
            if (breakCondition == 1) break;
            eTemp[j][i] = (1 / (uSquareSqrt[j])) * (uTemp[j][i]);
        }

        breakCondition = 1;

        if (iterationCounter == 0) {
            for (m = 0; m < arr[j].length; m++) {
                matrixDotProduct = aTemp[j + 1][m] * eTemp[j][m] + matrixDotProduct;
            }
        }
        else {
            for (m = 0; m < arr[j].length; m++) {
                for (s = 0; s <= iterationCounter; s++) {

                    matrixDotProduct = aTemp[j + 1][s] * eTemp[m][s] + matrixDotProduct;
                }
                for (t = 0; t < arr[j].length; t++) {
                    uTemp[j + 1][t] = aTemp[j + 1][t] - eTemp[j][t] * matrixDotProduct;

                }
            }
        }

        if (iterationCounter == 0) {
            for (m = 0; m < arr[j].length; m++) {
                uTemp[j + 1][m] = aTemp[j + 1][m] - eTemp[j][m] * matrixDotProduct;
            }
        }

        matrixDotProduct = 0;

        for (l = 0; l < arr[j].length; l++) {
            sumOfSquares = Math.pow(uTemp[j + 1][l], 2) + sumOfSquares;
        }

        uSquareSqrt[j + 1] = Math.sqrt(sumOfSquares);
        sumOfSquares = 0;

        for (i = 0; i < arr[j].length; i++) {
            eTemp[j + 1][i] = (1 / (uSquareSqrt[j + 1])) * (uTemp[j + 1][i]);
        }

        iterationCounter++;
    }
    qProjected = eTemp;
    return qProjected;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:59:17+00:00Added an answer on May 24, 2026 at 8:59 am

    I must apologize that instead of tweaking your code, I wrote my own from scratch:

    /* Main function of interest */
    
    // Each entry of a matrix object represents a column
    function gramSchmidt(matrixA, n) {        
        var totalVectors = matrixA.length;
    
        for (var i = 0; i < totalVectors; i++) {
            var tempVector = matrixA[i];
            for (var j = 0; j < i; j++) {
                var dotProd = dot(matrixA[i], matrixA[j], n);
                var toSubtract = multiply(dotProd, matrixA[j], n);
                tempVector = subtract(tempVector, toSubtract, n);
            }
            var nrm = norm(tempVector, n);
            matrixA[i] = multiply(1 / nrm, tempVector, n);
        }
    }
    
    /*
     * Example usage:
     * var myMatrix = [[1,0,0],[2,3,0],[5,4,7]];
     * gramSchmidt(myMatrix, 3);
     *   ==> myMatrix now equals [[1,0,0],[0,1,0],[0,0,1]]
     * 3 here equals the number of dimensions per vector
     */
    
    
    /* Simple vector arithmetic */
    
    function subtract(vectorX, vectorY, n) {
        var result = new Array(n);
        for (var i = 0; i < n; i++)
            result[i] = vectorX[i] - vectorY[i];
        return result;
    }
    
    function multiply(scalarC, vectorX, n) {
        var result = new Array(n);
        for (var i = 0; i < n; i++)
            result[i] = scalarC * vectorX[i];
        return result;
    }
    
    function dot(vectorX, vectorY, n) {
        var sum = 0;
        for (var i = 0; i < n; i++)
            sum += vectorX[i] * vectorY[i];
        return sum;
    }
    
    function norm(vectorX, n) {
        return Math.sqrt(dot(vectorX, vectorX, n));
    }
    

    Note that the algorithm above computes the Gram-Schmidt orthogonalization, which is the matrix [e1 | e2 | … | en], not the QR factorization!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to model a publications. A publication can have multiple authors and
I'm trying to model a network of Person s. A Person can have many
I'm trying to keep model logic within my model, but I can't get it
I am trying to model the UX for search as follows: How can I
I am trying to model a network using C++. I have a struct called
I'm trying to model which countries border each other in MySQL. I have three
I'm trying to model artists and songs and I have a problem where I
I am learning rails by trying to model a collectible card game. I have
I have a stored procedure that I've mapped in my entity framework model (trying
I'm trying to model an autonomic computing system. Can I use BIRT as the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.