Determines if an adjacency matrix is symmetric.
       Ref: http://mathworld.wolfram.com/SymmetricMatrix.html
355573
def is_adjacency_matrix_symmetric(adjacency_matrix):
    """"""
    # Verify that the matrix is square
    num_columns = len(adjacency_matrix)
    for column in adjacency_matrix:
        # In a square matrix, every row should be the same length as the number of columns
        if len(column) != num_columns:
            return False

    # Loop through the bottom half of the matrix and compare it to the top half
    # --We do the bottom half because of how we construct adjacency matrices
    max_i = 0
    for j in range(num_columns):
        for i in range(max_i):
            # If i == j, we can skip ahead so we don't compare with ourself
            if i == j:
                continue
            # Compare the value in the bottom half with the mirrored value in the top half
            # If they aren't the same, the matrix isn't symmetric
            if adjacency_matrix[j][i] != adjacency_matrix[i][j]:
                return False
        max_i += 1

    # If we reach this far without returning false, then we know that everything matched,
    # which makes this a symmetric matrix
    return True
305629
public static boolean svdcmp(double[][] a, int m, int n, double[] w, double[][] v) {

        boolean flag = false;

        int i, its, j, jj, k, l = 0, nm = 0;

        double anorm = 0.0, c, f, g = 0.0, h, s, scale = 0.0, x, y, z;

        double[] rv1 = new double[n];

        //Householder reduction to bidiagonal form
        for (i = 0; i < n; ++i) {

            l = i + 1;

            rv1[i] = scale * g;

            g = s = scale = 0.0;

            if (i < m) {

                for (k = i; k < m; ++k) {
                    scale += Math.abs(a[k][i]);
                }
                if (0.0 != scale) {
                    for (k = i; k < m; ++k) {
                        a[k][i] /= scale;
                        s += a[k][i] * a[k][i];
                    }
                    f = a[i][i];
                    g = sign(Math.sqrt(s), f);
                    h = f * g - s;
                    a[i][i] = f - g;
                    for (j = l; j < n; ++j) {

                        for (s = 0.0, k = i; k < m; ++k) {
                            s += a[k][i] * a[k][j];
                        }

                        f = s / h;

                        for (k = i; k < m; ++k) {
                            a[k][j] += f * a[k][i];
                        }

                    }

                    for (k = i; k < m; ++k) {
                        a[k][i] *= scale;
                    }

                }
            }

            w[i] = scale * g;

            g = s = scale = 0.0;

            if ((i < m) && (i != n - 1)) {

                for (k = l; k < n; ++k) {
                    scale += Math.abs(a[i][k]);
                }

                if (0.0 != scale) {

                    for (k = l; k < n; ++k) {
                        a[i][k] /= scale;
                        s += a[i][k] * a[i][k];
                    }

                    f = a[i][l];

                    g = -sign(Math.sqrt(s), f);

                    h = f * g - s;

                    a[i][l] = f - g;

                    for (k = l; k < n; ++k) {
                        rv1[k] = a[i][k] / h;
                    }

                    for (j = l; j < m; ++j) {
                        for (s = 0.0, k = l; k < n; ++k) {
                            s += a[j][k] * a[i][k];
                        }
                        for (k = l; k < n; ++k) {
                            a[j][k] += s * rv1[k];
                        }
                    }

                    for (k = l; k < n; ++k) {
                        a[i][k] *= scale;
                    }

                }

            }

            anorm = Math.max(anorm, (Math.abs(w[i]) + Math.abs(rv1[i])));
        }

        //Acumulation of right-hand transformation
        for (i = n - 1; i >= 0; --i) {

            if (i < n - 1) {

                if (0.0 != g) {

                    for (j = l; j < n; ++j) {
                        v[j][i] = (a[i][j] / a[i][l]) / g;
                    }

                    for (j = l; j < n; ++j) {

                        for (s = 0.0, k = l; k < n; ++k) {
                            s += a[i][k] * v[k][j];
                        }

                        for (k = l; k < n; ++k) {
                            v[k][j] += s * v[k][i];
                        }

                    }

                }

                for (j = l; j < n; ++j) {
                    v[i][j] = v[j][i] = 0.0;
                }

            }
            v[i][i] = 1.0;
            g = rv1[i];
            l = i;
        }

        //Acumulation of left-hand transformation
        for (i = Math.min(n, m) - 1; i >= 0; --i) {
            l = i + 1;
            g = w[i];
            for (j = l; j < n; ++j) {
                a[i][j] = 0.0;
            }
            if (0.0 != g) {

                g = 1.0 / g;

                for (j = l; j < n; ++j) {
                    for (s = 0.0, k = l; k < m; ++k) {
                        s += a[k][i] * a[k][j];
                    }
                    f = (s / a[i][i]) * g;
                    for (k = i; k < m; ++k) {
                        a[k][j] += f * a[k][i];
                    }
                }
                for (j = i; j < m; ++j) {
                    a[j][i] *= g;
                }
            } else {
                for (j = i; j < m; ++j) {
                    a[j][i] = 0.0;
                }
            }
            ++a[i][i];

        }

        //Diagonalization of the bidiagonal form
        for (k = n - 1; k >= 0; --k) {

            for (its = 1; its <= 30; ++its) {

                flag = true;

                for (l = k; l >= 0; --l) {
                    nm = l - 1;
                    if (Math.abs(rv1[l]) + anorm == anorm) {
                        flag = false;
                        break;
                    }
                    if (Math.abs(w[nm]) + anorm == anorm) {
                        break;
                    }
                }

                if (flag) {
                    c = 0.0;
                    s = 1.0;
                    for (i = l; i <= k; ++i) {
                        f = s * rv1[i];
                        if (Math.abs(f) + anorm == anorm) {
                            break;
                        }
                        g = w[i];
                        h = SVD.pythag(f, g);
                        w[i] = h;
                        h = 1.0 / h;
                        c = g * h;
                        s = -f * h;
                        for (j = 1; j <= m; ++j) {
                            y = a[j][nm];
                            z = a[j][i];
                            a[j][nm] = y * c + z * s;
                            a[j][i] = z * c - y * s;
                        }
                    }
                }

                //Convergence
                z = w[k];

                if (l == k) {
                    if (z < 0.0) {
                        w[k] = -z;
                        for (j = 0; j < n; ++j) {
                            v[j][k] = -v[j][k];
                        }
                    }
                    break;
                }

                if (30 == its) {
                    return false;
                }

                //Shift from bottom 2-by-2 minor
                x = w[l];
                nm = k - 1;
                y = w[nm];
                g = rv1[nm];
                h = rv1[k];
                f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
                g = pythag(f, 1.0);
                f = ((x - z) * (x + z) + h * ((y / (f + SVD.sign(g, f))) - h)) / x;

                //Next QR transformation
                c = s = 1.0;
                for (j = l; j <= nm; ++j) {
                    i = j + 1;
                    g = rv1[i];
                    y = w[i];
                    h = s * g;
                    g = c * g;
                    z = SVD.pythag(f, h);
                    rv1[j] = z;
                    c = f / z;
                    s = h / z;
                    f = x * c + g * s;
                    g = g * c - x * s;
                    h = y * s;
                    y *= c;
                    for (jj = 0; jj < n; ++jj) {
                        x = v[jj][j];
                        z = v[jj][i];
                        v[jj][j] = x * c + z * s;
                        v[jj][i] = z * c - x * s;
                    }
                    z = pythag(f, h);
                    w[j] = z;

                    if (0.0 != z) {
                        z = 1.0 / z;
                        c = f * z;
                        s = h * z;
                    }

                    f = c * g + s * y;
                    x = c * y - s * g;

                    for (jj = 0; jj < m; ++jj) {

                        y = a[jj][j];
                        z = a[jj][i];
                        a[jj][j] = y * c + z * s;
                        a[jj][i] = z * c - y * s;

                    }

                }

                rv1[l] = 0.0;
                rv1[k] = f;
                w[k] = x;

            }
        }

        return true;
    }
246255
public static DoubleMatrix[] symmetricEigenvectors(DoubleMatrix A) {
        A.assertSquare();
        DoubleMatrix eigenvalues = new DoubleMatrix(A.rows);
        DoubleMatrix eigenvectors = A.dup();
        int isuppz[] = new int[2 * A.rows];
        SimpleBlas.syevr('V', 'A', 'U', A.dup(), 0, 0, 0, 0, 0, eigenvalues, eigenvectors, isuppz);
        return new DoubleMatrix[]{eigenvectors, DoubleMatrix.diag(eigenvalues)};
    }
928517
public static int[] symmetricDifference(final int[] a, final int[] b) {
        if (N.isNullOrEmpty(a)) {
            return N.isNullOrEmpty(b) ? N.EMPTY_INT_ARRAY : b.clone();
        } else if (N.isNullOrEmpty(b)) {
            return a.clone();
        }

        return IntList.of(a).symmetricDifference(IntList.of(b)).trimToSize().array();
    }
244015
public static void checkMatrixMultiplication() {

        DoubleMatrix A = new DoubleMatrix(new double[][]{
                    {1.0, 2.0, 3.0},
                    {4.0, 5.0, 6.0},
                    {7.0, 8.0, 9.0}
                });
        DoubleMatrix E = new DoubleMatrix(new double[][]{
                    {0.0, 0.0, 1.0},
                    {0.0, 1.0, 0.0},
                    {1.0, 0.0, 0.0}
                });
        DoubleMatrix B = new DoubleMatrix(new double[][]{
                    {3.0, 2.0, 1.0},
                    {6.0, 5.0, 4.0},
                    {9.0, 8.0, 7.0}
                });

        check("checking matrix multiplication", A.mmul(E).equals(B));
    }
17136
def graph_from_adjacency_matrix(matrix, node_prefix='', directed=False):
    

    node_orig = 1

    if directed:
        graph = Dot(graph_type='digraph')
    else:
        graph = Dot(graph_type='graph')

    for row in matrix:
        if not directed:
            skip = matrix.index(row)
            r = row[skip:]
        else:
            skip = 0
            r = row
        node_dest = skip + 1

        for e in r:
            if e:
                graph.add_edge(
                    Edge(
                        node_prefix + node_orig,
                        node_prefix + node_dest))
            node_dest += 1
        node_orig += 1

    return graph
371693
private void _symmetricTql2 ()
  {
    // This is derived from the Algol procedures tql2, by
    // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
    // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
    // Fortran subroutine in EISPACK.
    for (int i = 1; i < m_nDim; i++)
      m_aEVe[i - 1] = m_aEVe[i];
    m_aEVe[m_nDim - 1] = 0.0;

    double f = 0.0;
    double tst1 = 0.0;
    for (int l = 0; l < m_nDim; l++)
    {
      // Find small subdiagonal element
      tst1 = Math.max (tst1, MathHelper.abs (m_aEVd[l]) + MathHelper.abs (m_aEVe[l]));
      int m = l;
      while (m < m_nDim)
      {
        if (MathHelper.abs (m_aEVe[m]) <= EPSILON * tst1)
          break;
        m++;
      }

      // If m == l, d[l] is an eigenvalue,
      // otherwise, iterate.

      if (m > l)
      {
        do
        {
          // Compute implicit shift
          double g = m_aEVd[l];
          double p = (m_aEVd[l + 1] - g) / (2.0 * m_aEVe[l]);
          double r = MathHelper.hypot (p, 1.0);
          if (p < 0)
          {
            r = -r;
          }
          m_aEVd[l] = m_aEVe[l] / (p + r);
          m_aEVd[l + 1] = m_aEVe[l] * (p + r);
          final double dl1 = m_aEVd[l + 1];
          double h = g - m_aEVd[l];
          for (int i = l + 2; i < m_nDim; i++)
          {
            m_aEVd[i] -= h;
          }
          f += h;

          // Implicit QL transformation.
          p = m_aEVd[m];
          double c = 1.0;
          double c2 = c;
          double c3 = c;
          final double el1 = m_aEVe[l + 1];
          double s = 0.0;
          double s2 = 0.0;
          for (int i = m - 1; i >= l; i--)
          {
            c3 = c2;
            c2 = c;
            s2 = s;
            g = c * m_aEVe[i];
            h = c * p;
            r = MathHelper.hypot (p, m_aEVe[i]);
            m_aEVe[i + 1] = s * r;
            s = m_aEVe[i] / r;
            c = p / r;
            p = c * m_aEVd[i] - s * g;
            m_aEVd[i + 1] = h + s * (c * g + s * m_aEVd[i]);

            // Accumulate transformation.

            for (int k = 0; k < m_nDim; k++)
            {
              h = m_aEigenVector[k][i + 1];
              m_aEigenVector[k][i + 1] = s * m_aEigenVector[k][i] + c * h;
              m_aEigenVector[k][i] = c * m_aEigenVector[k][i] - s * h;
            }
          }
          p = -s * s2 * c3 * el1 * m_aEVe[l] / dl1;
          m_aEVe[l] = s * p;
          m_aEVd[l] = c * p;

          // Check for convergence.

        } while (MathHelper.abs (m_aEVe[l]) > EPSILON * tst1);
      }
      m_aEVd[l] = m_aEVd[l] + f;
      m_aEVe[l] = 0.0;
    }

    // Sort eigenvalues and corresponding vectors.
    for (int i = 0; i < m_nDim - 1; i++)
    {
      int k = i;
      double p = m_aEVd[i];
      for (int j = i + 1; j < m_nDim; j++)
      {
        if (m_aEVd[j] < p)
        {
          k = j;
          p = m_aEVd[j];
        }
      }
      if (k != i)
      {
        m_aEVd[k] = m_aEVd[i];
        m_aEVd[i] = p;
        for (int j = 0; j < m_nDim; j++)
        {
          p = m_aEigenVector[j][i];
          m_aEigenVector[j][i] = m_aEigenVector[j][k];
          m_aEigenVector[j][k] = p;
        }
      }
    }
  }
385163
def isNonNegative(matrix):
    
    try:
        if (matrix >= 0).all():
            return True
    except (NotImplementedError, AttributeError, TypeError):
        try:
            if (matrix.data >= 0).all():
                return True
        except AttributeError:
            matrix = _np.array(matrix)
            if (matrix.data >= 0).all():
                return True
    return False
390126
def is_unitary(matrix: np.ndarray) -> bool:
    
    rows, cols = matrix.shape
    if rows != cols:
        return False
    return np.allclose(np.eye(rows), matrix.dot(matrix.T.conj()))
887347
def is_bool_matrix(l):
    

    """
    if isinstance(l, np.ndarray):
        if l.ndim == 2 and (l.dtype == bool):
            return True
    return False
1256470
public static void checkVectorAddition() {
        DoubleMatrix x = new DoubleMatrix(3, 1, 1.0, 2.0, 3.0);
        DoubleMatrix y = new DoubleMatrix(3, 1, 4.0, 5.0, 6.0);
        DoubleMatrix z = new DoubleMatrix(3, 1, 5.0, 7.0, 9.0);

        check("checking vector addition", x.add(y).equals(z));
    }
341757
def is_regular(self):
        

        """
        warn = False
        msg = 'The given matrix'

        # check symmetry
        if np.abs(self.A - self.A.T).sum() > 0:
            warn = True
            msg = '{} is not symmetric,'.format(msg)

        # check parallel edged
        if self.A.max(axis=None) > 1:
            warn = True
            msg = '{} has parallel edges,'.format(msg)

        # check that d is d-regular
        if np.min(self.d) != np.max(self.d):
            warn = True
            msg = '{} is not d-regular,'.format(msg)

        # check that g doesn't contain any self-loop
        if self.A.diagonal().any():
            warn = True
            msg = '{} has self loop.'.format(msg)

        if warn:
            self.logger.warning('{}.'.format(msg[:-1]))
1014128
public void factorize(boolean checkSymmetry) throws Exception{
		if (checkSymmetry && !Property.TWELVE.isSymmetric(Q)) {
			throw new Exception("Matrix is not symmetric");
		}
		
		if(this.rescaler != null){
			double[] cn_00_original = null;
			double[] cn_2_original = null;
			double[] cn_00_scaled = null;
			double[] cn_2_scaled = null;
			if(log.isDebugEnabled()){
				cn_00_original = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), Integer.MAX_VALUE);
				log.debug("cn_00_original Q before scaling: " + ArrayUtils.toString(cn_00_original));
				cn_2_original = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), 2);
				log.debug("cn_2_original Q before scaling : " + ArrayUtils.toString(cn_2_original));
			}
			//scaling the Q matrix, we have:
			//Q1 = U.Q.U[T] = U.L.L[T].U[T] = (U.L).(U.L)[T] 
			//and because U is diagonal it preserves the triangular form of U.L, so
			//Q1 = U.Q.U[T] = L1.L1[T] is the new Cholesky decomposition  
			DoubleMatrix1D Uv = rescaler.getMatrixScalingFactorsSymm(Q);
			if(log.isDebugEnabled()){
				boolean checkOK = rescaler.checkScaling(ColtUtils.fillSubdiagonalSymmetricMatrix(Q), Uv, Uv);
				if(!checkOK){
					log.warn("Scaling failed (checkScaling = false)");
				}
			}
			this.U = Uv;
			this.Q = (SparseDoubleMatrix2D) ColtUtils.diagonalMatrixMult(Uv, Q, Uv);
			if(log.isDebugEnabled()){
				cn_00_scaled = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), Integer.MAX_VALUE);
				log.debug("cn_00_scaled Q after scaling : " + ArrayUtils.toString(cn_00_scaled));
				cn_2_scaled = ColtUtils.getConditionNumberRange(new Array2DRowRealMatrix(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()), 2);
				log.debug("cn_2_scaled Q after scaling  : " + ArrayUtils.toString(cn_2_scaled));
				
				if(cn_00_original[0] < cn_00_scaled[0] || cn_2_original[0] < cn_2_scaled[0]){
					//log.info("Q: " + ArrayUtils.toString(ColtUtils.fillSubdiagonalSymmetricMatrix(Q).toArray()));
					log.warn("Problematic scaling");
					//throw new RuntimeException("Scaling failed");
				}
			}
		}
		
		double threshold = Utils.getDoubleMachineEpsilon();
		//elements of L are stored in a single array for i<diagonalLength, 
		//and with arrays of dimension i+1 for i>=diagonalLength
		this.LData = new double[dim - diagonalLength + 1][];
		double[] LData0 = new double[diagonalLength];
		LData[0] = LData0;
		
		if(dim - diagonalLength == 1 && Double.compare(Q.getQuick(dim-1, dim-1), 1.)==0){
			//TODO: the second condition can always be true if the matrix is normalized on the last element
			//closed form decomposition
			//@see "S.Boyd and L.Vandenberghe, Convex Optimization, p. 671"
			for (int i = 0; i < dim-1; i++) {
				double d = Q.getQuick(i, i);
				if (d < Utils.getDoubleMachineEpsilon()) {
					throw new Exception("not positive definite matrix");
				}
				LData0[i] = Math.sqrt(d);
			}
			//now the last row of L
			double[] LData1 = new double[dim];
			for(int j=0; j<dim - 1; j++){
				LData1[j] = Q.getQuick(dim-1, j) / LData0[j];
			}
			double d = 0;
			for(int k=0; k<diagonalLength; k++){
				d += Math.pow(Q.getQuick(dim-1, k), 2) / Q.getQuick(k, k);
			}
			LData1[dim-1] = Math.sqrt(1 - d);
			LData[1] = LData1;
		}else{
			for (int i = 0; i < dim; i++) {
			    if (i < diagonalLength) {
					double d = Q.getQuick(i, i);
					if(!(d > threshold)){
						throw new Exception("not positive definite matrix");
					}
					LData0[i] = Math.sqrt(d);
				} else {

					LData[i - diagonalLength + 1] = new double[i + 1];
					double[] LDataI = LData[i - diagonalLength + 1];

					// j < i
					for (int j = 0; j < diagonalLength; j++) {
						// here LData is not null only in its diagonal
						LDataI[j] = 1.0 / LData[0][j] * (Q.getQuick(i, j));
					}
					for (int j = diagonalLength; j < i; j++) {
						double[] LDataJ = LData[j - diagonalLength + 1];
						double sum = 0.0;
						for (int k = 0; k < j; k++) {
							sum += LDataI[k] * LDataJ[k];
						}
						LDataI[j] = 1.0 / LDataJ[j] * (Q.getQuick(i, j) - sum);
					}
					// j==i
					double sum = 0.0;
					for (int k = 0; k < i; k++) {
						sum += Math.pow(LDataI[k], 2);
					}
					double d = Q.getQuick(i, i) - sum;
					if (d < Utils.getDoubleMachineEpsilon()) {
						throw new Exception("not positive definite matrix");
					}
					LDataI[i] = Math.sqrt(d);
				}
			}
		}
	}
988949
function isMultiplicationOfEqualNodes(node) {
  if (!Node.Type.isOperator(node) || node.op !== '*') {
    return false;
  }

  // return if they are all equal nodes
  return node.args.reduce((a, b) => {
    return a.equals(b);
  });

}
1248522
def connectivity_matrix(cm):
    """"""
    # Special case for empty matrices.
    if cm.size == 0:
        return True
    if cm.ndim != 2:
        raise ValueError("Connectivity matrix must be 2-dimensional.")
    if cm.shape[0] != cm.shape[1]:
        raise ValueError("Connectivity matrix must be square.")
    if not np.all(np.logical_or(cm == 1, cm == 0)):
        raise ValueError("Connectivity matrix must contain only binary "
                         "values.")
    return True
665756
public static boolean isMatrix(DataBuffer shapeInfo) {
        int rank = Shape.rank(shapeInfo);
        if (rank != 2)
            return false;
        return !isVector(shapeInfo);
    }
1044061
public boolean isAcyclic() {

		// Tarjan's algorithm to detect strongly connected componenent of a graph
		final AbstractJobVertex[] reachable = getAllReachableJobVertices();
		final HashMap<AbstractJobVertex, Integer> indexMap = new HashMap<AbstractJobVertex, Integer>();
		final HashMap<AbstractJobVertex, Integer> lowLinkMap = new HashMap<AbstractJobVertex, Integer>();
		final Stack<AbstractJobVertex> stack = new Stack<AbstractJobVertex>();
		final Integer index = Integer.valueOf(0);

		for (int i = 0; i < reachable.length; i++) {
			if (!indexMap.containsKey(reachable[i])) {
				if (!tarjan(reachable[i], index, indexMap, lowLinkMap, stack)) {
					return false;
				}
			}
		}

		return true;
	}