recover a matrix to a higher dimension
195075
function(U) {
            	var k = this.cols();
            	var Ureduce = U.slice(1, U.rows(), 1, k);
            	return this.x(Ureduce.transpose());
                }
1357780
def _ring_2d(m, n):
  
  if m == 1:
    return [(0, i) for i in range(n)]
  if n == 1:
    return [(i, 0) for i in range(m)]
  if m % 2 != 0:
    tf.logging.warning("Odd dimension")
    return [(i % m, i // m) for i in range(n * m)]
  ret = [(0, 0)]
  for i in range(m // 2):
    for j in range(1, n):
      ret.append((2 * i, j))
    for j in range(n-1, 0, -1):
      ret.append((2 * i + 1, j))
  for i in range(m-1, 0, -1):
    ret.append((i, 0))
  return ret
1219007
def prune(matrix, threshold):
    
    if isspmatrix(matrix):
        pruned = dok_matrix(matrix.shape)
        pruned[matrix >= threshold] = matrix[matrix >= threshold]
        pruned = pruned.tocsc()
    else:
        pruned = matrix.copy()
        pruned[pruned < threshold] = 0

    # keep max value in each column. same behaviour for dense/sparse
    num_cols = matrix.shape[1]
    row_indices = matrix.argmax(axis=0).reshape((num_cols,))
    col_indices = np.arange(num_cols)
    pruned[row_indices, col_indices] = matrix[row_indices, col_indices]

    return pruned
43100
def split_last_dimension(x, n):
  
  x_shape = common_layers.shape_list(x)
  m = x_shape[-1]
  if isinstance(m, int) and isinstance(n, int):
    assert m % n == 0
  return tf.reshape(x, x_shape[:-1] + [n, m // n])
1150485
def adapt(self, d, x):
        
        # create input matrix and target vector
        self.x_mem[:,1:] = self.x_mem[:,:-1]
        self.x_mem[:,0] = x
        self.d_mem[1:] = self.d_mem[:-1]
        self.d_mem[0] = d
        # estimate output and error
        self.y_mem = np.dot(self.x_mem.T, self.w)
        self.e_mem = self.d_mem - self.y_mem
        # update
        dw_part1 = np.dot(self.x_mem.T, self.x_mem) + self.ide_eps
        dw_part2 = np.linalg.solve(dw_part1, self.ide)
        dw = np.dot(self.x_mem, np.dot(dw_part2, self.e_mem))
        self.w += self.mu * dw
1064023
def matrixToMathTransform(matrix):
    
    if isinstance(matrix, ShallowTransform):
        return matrix
    off, scl, rot = MathTransform(matrix).decompose()
    return ShallowTransform(off, scl, rot)
581176
def _rescale_to_unit_diagonals(mat):
    
    d = np.sqrt(np.diag(mat))
    mat /= d
    mat /= d[:, np.newaxis]
    return mat
913905
def matrix(self):
        """"""
        result = np.identity(4, float)
        result[0:3, 0:3] = self.r
        return result
88384
def locally_linear_embedding(self, num_dims=None):
    '''
    '''
    W = self.matrix()
    # compute M = (I-W)'(I-W)
    M = W.T.dot(W) - W.T - W
    if issparse(M):
      M = M.toarray()
    M.flat[::M.shape[0] + 1] += 1
    return _null_space(M, num_vecs=num_dims, overwrite=True)
657036
def gather_indices_2d(x, block_shape, block_stride):
  """"""
  # making an identity matrix kernel
  kernel = tf.eye(block_shape[0] * block_shape[1])
  kernel = reshape_range(kernel, 0, 1, [block_shape[0], block_shape[1], 1])
  # making indices [1, h, w, 1] to appy convs
  x_shape = common_layers.shape_list(x)
  indices = tf.range(x_shape[2] * x_shape[3])
  indices = tf.reshape(indices, [1, x_shape[2], x_shape[3], 1])
  indices = tf.nn.conv2d(
      tf.cast(indices, tf.float32),
      kernel,
      strides=[1, block_stride[0], block_stride[1], 1],
      padding="VALID")
  # making indices [num_blocks, dim] to gather
  dims = common_layers.shape_list(indices)[:3]
  if all([isinstance(dim, int) for dim in dims]):
    num_blocks = functools.reduce(operator.mul, dims, 1)
  else:
    num_blocks = tf.reduce_prod(dims)
  indices = tf.reshape(indices, [num_blocks, -1])
  return tf.cast(indices, tf.int32)
210279
def conv2d(x_input, w_matrix):
    """"""
    return tf.nn.conv2d(x_input, w_matrix, strides=[1, 1, 1, 1], padding='SAME')
1354877
def reshape_like_all_dims(a, b):
  """"""
  ret = tf.reshape(a, tf.shape(b))
  if not tf.executing_eagerly():
    ret.set_shape(b.get_shape())
  return ret
510090
def __unroll(self, rolled):
        """"""
        return np.array(np.concatenate([matrix.flatten() for matrix in rolled], axis=1)).reshape(-1)
395964
def as_matrix(self, depth=0):
        
        if depth in self._matrix_cache:
            return self._matrix_cache[depth]
        self._matrix_cache[depth] = matrix = Matrix(self, depth=depth)
        return matrix
168279
def projection_matrix(w):
    ''''''
    return np.identity(3) - np.dot(np.reshape(w, (3,1)), np.reshape(w, (1, 3)))
829424
def apply_matrix_norm(m, v):
    """"""
    (a, b, c, d, e, f) = m
    (p, q) = v
    return (a*p+c*q, b*p+d*q)
259167
def clean(matrix):
    """"""
    return defaultdict(lambda: ' ', {
        k: v for k, v in matrix.items() if v != ' '
    })