python - 图中的并联电阻(电路的节点表示)
问题描述
所以我有这个对象:电阻器、电压源、节点和图表。我的主要目标是从电路中表示一些图表,我遇到了这个问题:当我添加从 Node2 到 NodeRef 的并行节点的权重时,显然 R2 电阻被 R3 电阻取代。有什么建议可以在没有多图的情况下实现这个吗?
def create_components(self):
NodeRef = Node("0")
Node1 = Node("1")
Node2 = Node("2")
R1 = Resistor("R1", 100) #R1.get_val() returns the value in ohms
R2 = Resistor("R2", 200)
R3 = Resistor("R3", 300)
V1 = Voltage("Source", 5)
NodeRef.add_destination(Node1, 0) # Node.add_destination(destination, weight)
Node1.add_destination(Node2, R1.get_val())
Node2.add_destination(NodeRef, R2.get_val())
Node2.add_destination(NodeRef, R3.get_val())
因为我还不能发布图片,所以电路原理图是这样的:
|-------R1------------
| | |
V1 R2 R3
| | |
|---------------------
我想用图表表示的电路:
解决方案
跟踪两个节点之间所有电导的总和。电导是电阻的倒数。平行电导正常相加。要获得净电阻,只需返回总跟踪电导和的倒数。
class Node():
def __init__(self,label):
self.label = label
self.connections = {}
def add_destination(self,other,weight):
if not other.label in self.connections:
self.connections[other.label] = 0
self.connections[other.label] += (1.0/weight if weight !=0 else float('inf'))
other.connections[self.label] = self.connections[other.label]
def resistance(self,other):
if other.label in self.connections and self.connections[other.label] != 0:
return 1.0/self.connections[other.label]
return float('inf')
推荐阅读
- asp.net-mvc-4 - MVC4 Razor 在单个视图上访问多个模型类在各种元素上给出错误
- django - 在有和没有时区的 Django 中处理多个数据库
- html - 跨元素的舍入固定表格布局单元格宽度百分比计算
- python - NumPy 在二维数组中逐行搜索一维数组
- docusignapi - 是否有 eSignature REST API 中可用的所有 XSD 请求/响应的列表?
- python - Python 脚本无法从 XML 中提取数据
- docker - Docker撰写端口映射,主机端口!=容器端口
- python - 需要帮助将时间戳正确传递给字典
- android - 显示此应用的 Google Play 与此版本不兼容
- python - 如何在循环内执行异步函数而不多次调用它?