我需要的是Code i应该替换它:ZeroMQ
with。
我有一个在表格中创建单元格的类。我将这些单元格存储在单元格列表中。
inproc:
我想知道该列表中是否存在单元格。我正在做以下事情:
<-- code (exist) -->
细胞分类
List<Cell> locations = new List<Cell>();
答案 0 :(得分:3)
使用IEqualityComparer<Cell>
的推动力让您的生活变得轻松。
我使用这个解决方案,因为你可能需要在某个地方使用那个seme逻辑。
public class CellComparer : IEqualityComparer<Cell>
{
public bool Equals(Cell x, Cell y)
{
if (x == null && y == null) return true;
if (x == null || y == null) return false;
if (x.Column == y.Column && x.Row == y.Row) return true;
return false;
}
public int GetHashCode(Cell cell)
{
int hCode = cell.Column ^ cell.Row;
return hCode.GetHashCode();
}
}
要使用它只是简单的,如果你检查列表内容,你会看到它包含两个元素,因为第一个和最后一个添加的单元格是相同的。
var list = new HashSet<Cell>(new CellComparer());
list.Add(new Cell(0, 1));
list.Add(new Cell(1, 2));
list.Add(new Cell(0, 1));
感谢HashSet
,CellComparer
将使用您的Cell
来避免public class Cell
{
public Cell(int col, int row)
{
Column = col;
Row = row;
}
public int Row { get; private set; }
public int Column { get; private set; }
public int[] Position
{
get { return new[] { Column, Row }; }
}
}
重复。
因此,使用auto-property而不是method来返回字段的值。您的单元格类必须如下所示:
from ivisual import *
from numpy.linalg import solve
import numpy as np
import math as m
scene = display(x=0,y=0,width=500,height=500,
title='String and masses configuration')
tempe = curve(x=range(0,500),color=color.black)
n = 9
eps = 1*10**(-6)
deriv = np.zeros( (n, n), float)
f = np.zeros( (n), float)
x = np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1., 1., 1.])
def plotconfig():
for obj in scene.objects:
obj.visible=0 # to erase the previous configuration
L1 = 3.0
L2 = 4.0
L3 = 4.0
xa = L1*x[3] # L1*cos(th1)
ya = L1*x[0] # L1 sin(th1)
xb = xa+L2*x[4] # L1*cos(th1)+L2*cos(th2)
yb = ya+L2*x[1] # L1*sin(th1)+L2*sen(th2)
xc = xb+L3*x[5] # L1*cos(th1)+L2*cos(th2)+L3*cos(th3)
yc = yb-L3*x[2] # L1*sin(th1)+L2*sen(th2)-L3*sin(th3)
mx = 100.0 # for linear coordinate transformation
bx = -500.0 # from 0=< x =<10
my = -100.0 # to -500 =<x_window=>500
by = 400.0 # same transformation for y
xap = mx*xa+bx # to keep aspect ratio
yap = my*ya+by
ball1 = sphere(pos=(xap,yap), color=color.cyan,radius=15)
xbp = mx*xb+bx
ybp = my*yb+by
ball2 = sphere(pos=(xbp,ybp), color=color.cyan,radius=25)
xcp = mx*xc+bx
ycp = my*yc+by
x0 = mx*0+bx
y0 = my*0+by
line1 = curve(pos=[(x0,y0),(xap,yap)], color=color.yellow,radius=4)
line2 = curve(pos=[(xap,yap),(xbp,ybp)], color=color.yellow,radius=4)
line3 = curve(pos=[(xbp,ybp),(xcp,ycp)], color=color.yellow,radius=4)
topline = curve(pos=[(x0,y0),(xcp,ycp)], color=color.red,radius=4)
def F(x, f): # Define F function
f[0] = 3*x[3] + 4*x[4] + 4*x[5] - 8.0
f[1] = 3*x[0] + 4*x[1] - 4*x[2]
f[2] = x[6]*x[0] - x[7]*x[1] - 10.0
f[3] = x[6]*x[3] - x[7]*x[4]
f[4] = x[7]*x[1] + x[8]*x[2] - 20.0
f[5] = x[7]*x[4] - x[8]*x[5]
f[6] = pow(x[0], 2) + pow(x[3], 2) - 1.0
f[7] = pow(x[1], 2) + pow(x[4], 2) - 1.0
f[8] = pow(x[2], 2) + pow(x[5], 2) - 1.0
def dFi_dXj(x, deriv, n): # Define derivative function
h = 1*10**(-4)
for j in range(0, n):
temp = x[j]
x[j] = x[j] + h/2.
F(x, f)
for i in range(0, n): deriv[i, j] = f[i]
x[j] = temp
for j in range(0, n):
temp = x[j]
x[j] = x[j] - h/2.
F(x, f)
for i in range(0, n): deriv[i, j] = (deriv[i, j] - f[i])/h
x[j] = temp
for it in range(1, 100):
rate(1) # 1 second between graphs
F(x, f)
dFi_dXj(x, deriv, n)
B = np.array([[-f[0]], [-f[1]], [-f[2]], [-f[3]], [-f[4]], [-f[5]],[-f[6]], [-f[7]], [-f[8]]])
sol = solve(deriv, B)
dx = np.take(sol, (0, ), 1) # take the first column of matrix sol
for i in range(0, n):
x[i] = x[i] + dx[i]
plotconfig()
errX = errF = errXi = 0.0
for i in range(0, n):
if ( x[i] != 0.): errXi = abs(dx[i]/x[i])
else: errXi = abs(dx[i])
if ( errXi > errX): errX = errXi
if ( abs(f[i]) > errF ): errF = abs(f[i])
if ( (errX <= eps) and (errF <= eps) ): break
print('Number of iterations = ', it)
print('Solution:')
for i in range(0, n):
print('x[', i, '] = ', x[i])
答案 1 :(得分:2)
您可以将IEquatable
接口添加到Cell类:
public class Cell : IEquatable<Cell>
在课程中需要bool
方法Equals
:
public bool Equals(Cell otherCell)
,在this
单元格等于otherCell
(例如,this.getRow() == otherCell.getRow()
等)时,您可以提供代码告诉。
如果Cell
为IEquatable
,则允许您在单元格列表中使用Contains
方法。因此,在您的主要代码中,您可以使用<-- code (exist) -->
而不是locations.Contains(cell)
。
你应该考虑使用属性重写你的Cell
课程。
答案 2 :(得分:1)
你可以使用linq来做那个
public bool cell_in_array(Cell cell)
{
return locations.Any(c => c.getColumn() == cell.getColumn() &&
c.getRow() == cell.getRow())
}
Any
将确定是否有任何元素符合条件。
答案 3 :(得分:0)
您可以使用Any
来确定序列中的任何元素是否满足条件:
locations.Any(c => c.getColumn() == cell.getColumn() && c.getRow() == cell.getRow())