
// -----------------------------------------------------------------------------------
function countnums(a, n) {   // Count the numbers in the first n entries of an array a

    var cc = 0;
    for (var ii=0;ii<n; ii++) {
        if (!isNaN(a[ii])) {cc++;}
    }
    return cc;
}  // End function countnums(a, n)


// -----------------------------------------------------------------------------------
function dump2D (a) {  // dump a 2-d array a into an alert
    for (var di=0; di<a.length; di++) {
        alert("di = " + di + " a[di] = " + a[di] + "\n");
    }
}  // End function dump2D (a)



// -----------------------------------------------------------------------------------
function setup_lsmatrix(scores, numd) {   // scores is the matrix of scores
                                          // numd is the number of rows and columns to use
                                          
    var team1;                         
    var lsmatrix = [];  // Least-squares matrix: each of the 2*numd entries is a row of 2*numd+1 numbers,
                                    // representing a linear equation constraining one team's
                                    // offensive or defensive "strength".
                                    
                                    // Let o[team1] represent the offensive strength of team1
                                    // and d[team1] represent the defensive strength of team1.
                                    // It is those two vectors of numbers that we seek to find by solving
                                    //   the set of linear equations defined by the lsmatrix[].
                                    
                                    // Given those vectors, we can compute the predicted score
                                    //   of a game between team i and team j:
                                    //   team i should score o[i]-d[j] points
                                    //   and team j should score o[j]-d[i] points.
                                    
                                    // The first numd rows of lsmatrix[] represent offensive strengths.
                                    // The last numd rows represent defensive strengths.
                                    // The first numd columns in a row correspond to offensive
                                    //   strengths of opponent teams.
                                    // The next numd columns in a row correspond to defensive
                                    //   strengths of opponent teams.
                                    // The final column represents the total points scored by
                                    //   the team (top half of matrix),
                                    //   or the total points scored against the team (bottom half of matrix).
                                    // Solving the matrix yields a set of numd offensive strengths o[i]
                                    //   and a set of numd defensive strengths d[i], one offensive strength
                                    //   for each team and one defensive strength for each team.
                                                                
                                
    //  Set every entry of lsmatrix to zero:
    for (team1=0; team1< 2*numd; team1++) {
            lsmatrix[team1] = [];
            for (var col=0; col<2*numd+1; col++) {
                lsmatrix[team1][col] = 0;
            }
    }
    
    
    // Fill in the non-zero entries of lsmatrix:
    
    // Upper left quadrant:  Each diagonal element is the number of games played by team1
    // Lower right quadrant: Each diagonal element is the negative of the number of games played by team1
    for (team1=0; team1< numd; team1++) {  
        var cnums = countnums(scores[team1], numd);
        var nt = numd + team1;

        lsmatrix[team1][team1] = cnums;
        lsmatrix[nt][nt] = -cnums;
        
    }

    // alert("Here is upper left and lower right:");
    // dump2D(lsmatrix); 

    // Upper right quadrant:  Place -1 in the column for each team that team1 played
    //   and place the sum of the scores for team1 in the last column of the row
    // Lower left quadrant: Place 1 in the column for each team that team1 played
    //   and place the sum of the scores against team1 in the last column of the row
    for (team1=0; team1< numd; team1++) {
        var points_for_team1 = 0;
        var points_against_team1 = 0;
        for (var team2=0; team2<numd; team2++) {
            if ( !isNaN(scores[team1][team2]) ) {
                lsmatrix[team1][numd+team2] = -1;
                lsmatrix[numd+team1][team2] = 1;
                points_for_team1 += scores[team1][team2];
                points_against_team1 += scores[team2][team1];
            }
        }
        lsmatrix[team1][2*numd] = points_for_team1;
        lsmatrix[numd+team1][2*numd] = points_against_team1;  
    }


    // alert("Here is the whole thing:");
    // dump2D(lsmatrix); 


    return lsmatrix;
      
}  // End of function setup_lsmatrix(scores)
