matrix

C#:桥接模式

邮差的信 提交于 2020-03-03 18:18:50
转自原文 C#: 桥接模式 1.引言 桥接模式是一种很实用的结构型设计模式,如果系统中的某各类存在两个独立变化的纬度,通过桥接模式可以将这两个纬度分离出来,它是将抽象部分和实现部分解耦,使得两者都能够独立变化。如果不理解,怎么说都会觉得晦涩,看下面的两个例子: (1)示例1【 桥接模式 】 把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体电视品牌的遥控器都继承这个抽象类,具体设计类图如下: 这样的实现使得每部不同型号的电视都有自己遥控器实现,这样的设计对于电视机的改变可以很好地应对,只需要添加一个派生类就搞定了,但随着时间的推移,用户需要改变遥控器的功能,如:用户可能后面需要对遥控器添加返回上一个台等功能时,此时上面的设计就需要修改抽象类RemoteControl的提供的接口了,此时可能只需要向抽象类中添加一个方法就可以解决了,但是这样带来的问题是我们改变了抽象的实现,如果用户需要同时改变电视机品型号和遥控器功能时,上面的设计就会导致相当大的修改,显然这样的设计并不是好的设计。 (2)示例2 画笔有毛笔和蜡笔两种,假如需要大、中、小3种型号的画笔,能够分别绘制出12种不同的颜色,如果使用蜡笔的话需要准备36支,如果使用毛笔,则只需要准备3种型号的毛笔外加12种颜色的调色板,总共15样。如果需要新增一种新型号的画笔,对于蜡笔来说需要增加12支

Extract values from a correlation matrix according to their p-value in a second matrix

淺唱寂寞╮ 提交于 2020-03-03 10:14:44
问题 I have created a correlation matrix with an external program (SparCC). I have calculated p-values from the same data in SparCC as well and I end up with two objects which I imported into R, let's call them corr and pval and > ncol(corr)==nrow(corr) [1] TRUE > ncol(pval)==nrow(pval) [1] TRUE and > colnames(corr)==rownames(pval) [1] TRUE ... and the same the other way around. Since the matrices (or should I be using data.frame ?) are fairly large (about 1000 items), I would like to extract the

Extract values from a correlation matrix according to their p-value in a second matrix

牧云@^-^@ 提交于 2020-03-03 10:14:09
问题 I have created a correlation matrix with an external program (SparCC). I have calculated p-values from the same data in SparCC as well and I end up with two objects which I imported into R, let's call them corr and pval and > ncol(corr)==nrow(corr) [1] TRUE > ncol(pval)==nrow(pval) [1] TRUE and > colnames(corr)==rownames(pval) [1] TRUE ... and the same the other way around. Since the matrices (or should I be using data.frame ?) are fairly large (about 1000 items), I would like to extract the

Why is the z coordinate flipped after multiplying with a matrix in GLSL - OpenGL

僤鯓⒐⒋嵵緔 提交于 2020-03-03 07:03:06
问题 I'm making a small game engine in which i want to draw stuff using OpenGL. I abstracted all the OpenGL objects into classes (Buffers, VertexArrays, Shaders, Programs...). Everything worked fine until i got to 3D rendering. I implemented my own matrices and vectors(i didn't use like glm), and when i multiply my vertex position in the shader with any matrix, the z coordinate flips (z = -z). I even tried with the identity matrix. Here is the vertex shader: #version 330 core layout(location = 0)

numpy: broadcast matrix multiply accross array

妖精的绣舞 提交于 2020-03-03 06:55:35
问题 I have a 3xN array, conceptually an array of N 3-vectors, I want to construct the array which results from matrix multiplying a given 3x3 matrix with each column of the array. Is there a good way to do this in a vectorized manner? Currently, my problem is 3xN , but I may need to consider 3xNxM (or more) in the future. Loopy approach U=numpy.rand( [3,24] ) R=numpy.eye(3) # placeholder for i in xrange( U.shape[1]): U[:,i]=numpy.dot( R, U[:,i] ) 回答1: Using np.einsum function you can do it even

numpy: broadcast matrix multiply accross array

雨燕双飞 提交于 2020-03-03 06:53:13
问题 I have a 3xN array, conceptually an array of N 3-vectors, I want to construct the array which results from matrix multiplying a given 3x3 matrix with each column of the array. Is there a good way to do this in a vectorized manner? Currently, my problem is 3xN , but I may need to consider 3xNxM (or more) in the future. Loopy approach U=numpy.rand( [3,24] ) R=numpy.eye(3) # placeholder for i in xrange( U.shape[1]): U[:,i]=numpy.dot( R, U[:,i] ) 回答1: Using np.einsum function you can do it even

leetcode 221

岁酱吖の 提交于 2020-03-02 21:51:37
题意:给定一个0,1矩阵,找到最大的包含1的正方形,并返回它的面积。 思路: 动态规划 初始化:二维数组:dp[i][j] 表示 到达(i, j )位置所能组成的最大正方形的边长。 1)边界条件:i表示行数,j表示列数。 i == 0 || j == 0 2)状态转移方程:matrix[i][j] == 1时,dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j] ) ) +1 class Solution { public: int maximalSquare(vector<vector<char>>& matrix) { if(matrix.empty() || matrix[0].empty()) return 0; int n = matrix.size(); int m = matrix[0].size(); int dp[n][m] = {}; //最长边长 int res = 0; //面积 for(int i=0; i<n; i++){ for(int j =0;j<m; j++){ if(i==0 || j==0) dp[i][j] = matrix[i][j] - '0'; else if(matrix[i][j] == '1'){ dp[i][j] = min(dp[i-1][j-1], min(dp[i

221. Maximal Square(dp)

ぃ、小莉子 提交于 2020-03-02 21:51:16
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 用dp来记录以i,j为右下角元素时最大的matrix。状态转移方程比较trick Solution: class Solution(object): def maximalSquare(self, matrix): """ :type matrix: List[List[str]] :rtype: int """ res = 0 dp = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))] for i in range(len(matrix)): for j in range(len(matrix[0])): dp[i][j] = 1 if matrix[i][j]=='1' else 0 if i>0 and j>0 and matrix[i][j]=='1': dp[i][j] = 1 + min(dp[i-1][j-1],min(dp[i-1]

python求度分布的程序

蓝咒 提交于 2020-03-02 07:33:56
使用python求度分布的程序 # -*- coding: utf-8 -*- """ Created on Sun Jun 16 20:13:42 2019 @author: Administrator """ """ 这个函数的作用是将一个矩阵给转换成一个图, 矩阵以多维列表的形式存在,即列表的列表 此处的转换是针对无向图 根据邻接矩阵得到图之后,我们就可以调用networkx 里面的各种函数来分析图的性质,比如度分布, 平均路径程度,聚类系数等一系列图的拓扑性质 """ import networkx as nx import matplotlib.pyplot as plt def degree_of_graph(): # reload(sys) G = nx.Graph() filestr = "" with open("matrix.txt") as files: for line in files: filestr += line.strip() #将字符串转换成列表 matrix = eval(filestr) #存储y坐标的值 y_value = [] #存储x坐标的值 x_value = [] nodes = range(len(matrix)) G.add_nodes_from(nodes) for i in range(len(matrix)): for j

python求解接近中心性

被刻印的时光 ゝ 提交于 2020-03-02 04:11:20
求解网络科学里面的接近中心性 import matplotlib.pyplot as plt import networkx as nx import operator def get_closeness_centrality(): G = nx.Graph() filestr = "" #需要读入的邻接矩阵 with open("nsMatrix.txt") as files: for line in files: filestr += line.strip() #将字符串转换成列表 matrix = eval(filestr) nodes = range(len(matrix)) G.add_nodes_from(nodes) for i in range(len(matrix)): for j in range(len(matrix)): if(matrix[i][j] == 1): G.add_edge(i, j) cc = nx.algorithms.bipartite.centrality.closeness_centrality(G,G.node()) print("节点编号及其接近中心性最大值为:") cc_list = sorted(cc.items(), key=operator.itemgetter(1)) print(cc_list) get