Python weird 2d matrix behaviour

Why does this code:

matrix = []

for a in range(2):
  matrix.append([])
  for b in range(2):
    matrix[a].append(a)

print(matrix)

Outputs this:

[
  [0, 0],
  [1, 1]
]

But this code:

matrix = [[None] * 2] * 2

for a in range(2):
  for b in range(2):
    matrix[a][b] = a

print(matrix)

Outputs this:

[
  [1, 1],
  [1, 1]
]

In the second example wherever we assing ‘a’ in the matrix it changes to the last value of ‘a’ at the end of the loop. I don’t quite understand this behaviour. If some one can explain it please.

If I want to fill a matrix with the number of the row and column of each cell, what would be the best aproach? I feel like calling .append() it’s not the best when we already know the size of the matrix. Since creating the matrix with:

matrix = [[None] * col] * row

and then filling it doesn’t work as I expected. What would be the best approach?

Something like this?

matrix = [[str(a)+str(b) for b in range(2)] for a in range(2)]

Graph implementation in c++ using adjacency matrix

Here is my code for implementing Graph using adjacency matrix. The code should be Object-oriented and there is a method isPath() checking if there is a connection between two nodes. Some advice?

#include 
#include 
#include  

using namespace std;

class Graph
{
protected:
    int value;
    int **graphm;

public:
    Graph(int value)
    {
        this->value = value;
        graphm = new int*[value];
        int k, j;
        for (k = 0; k < value; k++)
        {
            graphm[k] = new int[value];
            for (j = 0; j < value; j++)
            {
                graphm[k][j] = 0;
            }
        }
    }

    void newEdge(int head, int end)
    {
        if (head > value || end > value || head < 0 || end < 0)
        {
            cout << "Invalid edge!n";
        }
        else
        {
            graphm[head - 1][end - 1] = 1;
            graphm[end - 1][head - 1] = 1;
        }
    }

    void display()
    {
        int i, p;

        for (i = 0; i < value; i++)
        {
            for (p = 0; p < value; p++)
            {
                cout << graphm[i][p] << "   ";
            }
            cout << endl;
        }
    }

    void isPath(int head, int end)
    {
        int k, o;
        ofstream fullstore("Test.txt");

        cout << graphm[head - 1][end - 1];
        cout << endl;
        if (!fullstore.is_open())
        {
            cout << "File can't be open";
        }

        if (graphm[head - 1][end - 1] == 1)
        {
            cout << "There is an edge between " << head << " and " << end << "n";
            fullstore << head << ", " << end;
            fullstore.close();
        }
        else
        {
            cout << "Edge not foundn";
        }
    }
};

int main()
{
    int vertex, numberOfEdges, i, head, end;
    cout << "Enter number of nodes: ";
    cin >> vertex;
    numberOfEdges = vertex * (vertex - 1);
    Graph g1(vertex);
    for (int i = 0; i < numberOfEdges; i++)
    {
        cout << "Enter edge ex.1 2 (-1 -1 to exit): n";
        cin >> head >> end;
        if ((head == -1) && (end == -1))
        {
            break;
        }
        g1.newEdge(head, end);
    }
    g1.display();
    cout << endl;
    g1.isPath(1, 3);

    return 0;
}

Is a company with Tech Leads a matrix organization?

Employees of my company are grouped into pools depending on technical skills (e.g. front-end devs, back-end devs, QA engineers, etc). For each pool there’s a Tech Lead who doesn’t manage projects (or people) but provides general technical directions to the company: he decides technical stacks, standard solutions, best practices and so on.

To initiate a project, the Head of Delivery selects a PM and staffs a team by taking technicians from the pools (depending on the requirements). The PM has full authority over his colleagues except for the technical decisions.

Is my company still a projectized organization or do the Tech Leads make it a matrix organization?

Thank you very much

How to write a matrix of $ntimes 2$ where the first column is made of only 1’s?

How to write a matrix of $ntimes 2$ where the first column is made of only 1’s?

Suppose I have a vector x={.24,…,10}.

How can I get this

$left(
begin{matrix}
1 & .24 \
1 & 21 \
1 & 33 \
1 & 11 \
1 & 10
end{matrix}right)$

How can I do that?

Help me please

I know how to write a ‘normal’ matrix, it’s MatrixForm[{1,2},{1,1}] but how to give the form above?

How to interpret upper-triangular matrix of cosine similarities

in Spark, there is a RowMatrix.columnSimilarities() method (see http://spark.apache.org/docs/latest/api/java/org/apache/spark/mllib/linalg/distributed/RowMatrix.html#columnSimilarities()) that returns “An n x n sparse upper-triangular matrix of cosine similarities between columns of this matrix“.

How should I read it? If I try to implement an example from https://stackoverflow.com/a/1750187 as following:

JavaRDD rows = sc.parallelize(Arrays.asList(
    new DenseVector(new double[]{2, 1, 0, 2, 0, 1, 1, 1}),
    new DenseVector(new double[]{2, 1, 1, 1, 1, 0, 1, 1})
));

RowMatrix mat = new RowMatrix(rows.rdd());  
List sims = mat.columnSimilarities().toRowMatrix().rows().toJavaRDD().collect();
for(Vector v: sims) {
    System.out.println(v);
}

I get this

(8,[6,7],[0.7071067811865475,0.7071067811865475])
(8,[1,2,3,4,5,6,7],[0.9999999999999998,0.7071067811865475,0.9486832980505137,0.7071067811865475,0.7071067811865475,0.9999999999999998,0.9999999999999998])
(8,[2,3,4,5,6,7],[0.7071067811865475,0.9486832980505137,0.7071067811865475,0.7071067811865475,0.9999999999999998,0.9999999999999998])
(8,[7],[0.9999999999999998])
(8,[4,5,6,7],[0.4472135954999579,0.8944271909999159,0.9486832980505137,0.9486832980505137])
(8,[6,7],[0.7071067811865475,0.7071067811865475])
(8,[3,4,6,7],[0.4472135954999579,1.0,0.7071067811865475,0.7071067811865475])

How should I interpret it? How do I get the cosine angle 0.822 from this, as mentioned in the referenced StackOverflow post?

Thanks!

Homomorphisms of a matrix ring.

I have the following question, What are all possible right $R$-module $(R = mathbb{M}_2(mathbb{Z}))$ homomorphisms from $mathbb{M}_2(nmathbb{Z})$ to $mathbb{M}_2(mathbb{Z})$. I tried to extend maps from $nmathbb{Z}$ to $mathbb{Z}$ defined as $f_{n,a}(nx) = ax$ and found that these are right module homomorphisms. But are these the only ones like in case of $mathbb{Z}$ ??

Finding all the solutions of some matrix equations

I am trying to find all possible solutions to the following:

A = ( {
    {a11, a12},
    {a21, a22}
   } );
B = ( {
    {b11, b12},
    {b21, b22}
   } );

A.A
{{a11^2 + a12 a21, a11 a12 + a12 a22}, {a11 a21 + a21 a22, a12 a21 + a22^2}}
A.B
{{a11 b11 + a12 b21, a11 b12 + a12 b22}, {a21 b11 + a22 b21, a21 b12 + a22 b22}}
B.A
{{a11 b11 + a21 b12, a12 b11 + a22 b12}, {a11 b21 + a21 b22, a12 b21 + a22 b22}}
B.B
{{b11^2 + b12 b21, b11 b12 + b12 b22}, {b11 b21 + b21 b22, b12 b21 + b22^2}}

but I am unable to find all solutions, I have found few solutions using:

Solve[
  {a11^2 + a12 a21 == a11, a11 a12 + a12 a22 == a12, 
   a11 a21 + a21 a22 == a21, a12 a21 + a22^2 == a22, 
   a11 b11 + a12 b21 == b11, a11 b12 + a12 b22 == b12, 
   a21 b11 + a22 b21 == b21, a21 b12 + a22 b22 == b22, 
   a11 b11 + a21 b12 == b11, a12 b11 + a22 b12 == b12, 
   a11 b21 + a21 b22 == b21, a12 b21 + a22 b22 == b22, 
   b11^2 + b12 b21 == a11, b11 b12 + b12 b22 == a12, 
   b11 b21 + b21 b22 == a21, b12 b21 + b22^2 == a22}, 
  {a11, a12, a21, a22, b11, b12, b21, b22}]

but not all. Hence, I want to find all solutions.

Also I am trying to solve a similar problem in the 5×5 case. I type the following:

A = ( {
    {a11, a12, a13, a14, a15},
    {a21, a22, a23, a24, a25},
    {a31, a32, a33, a34, a35},
    {a41, a42, a43, a44, a45},
    {a51, a52, a53, a54, a55}
   } );
B = ( {
    {b11, b12, b13, b14, b15},
    {b21, b22, b23, b24, b25},
    {b31, b32, b33, b34, b35},
    {b41, b42, b43, b44, b45},
    {b51, b52, b53, b54, b55}
   } );
C = ( {
    {c11, c12, c13, c14, c15},
    {c21, c22, c23, c24, c25},
    {c31, c32, c33, c34, c35},
    {c41, c42, c43, c44, c45},
    {c51, c52, c53, c54, c55}
   } );
D = ( {
    {d11, d12, d13, d14, d15},
    {d21, d22, d23, d24, d25},
    {d31, d32, d33, d34, d35},
    {d41, d42, d43, d44, d45},
    {d51, d52, d53, d54, d55}
   } );
E = ( {
    {e11, e12, e13, e14, e15},
    {e21, e22, e23, e24, e25},
    {e31, e32, e33, e34, e35},
    {e41, e42, e43, e44, e45},
    {e51, e52, e53, e54, e55}
   } );

Solve[
  {A.A == A, A.B == B, A.C == C, A.D == D, A.E == E, B.A == B, B.B == C, 
   B.C == D, B.D == E, B.E == A, C.A == C, C.B == D, C.C == E, C.D == A, 
   C.E == B, D.A == D, D.B == E, D.C == A, D.D == B, D.E == C, E.A == E, 
   E.B == A, E.C == B, E.D == C, E.E == D}

But it shows a red thing on the right hand side. What is wrong?