<h2>更多(num)Python</h2>
<p>在python中,您可以编写</p>
<pre><code> n = len(stresses)
forcej = zeros( n ) # Initialize Forces in current increment
for i, stress in enumerate(stresses):
forcej[i] = area*stress
</code></pre>
<p>更优雅的是,作为一个列表:</p>
<pre><code> forcej = array([ area * stress for stress in stresses ])
</code></pre>
<p>(请注意,python中的局部变量按约定在<code>lowercase</code>或<code>snake_case</code>中命名。)</p>
<p>但这是NumPy代码,所以让我们用标量向量乘法代替:</p>
<pre><code> forcej = area * stresses
</code></pre>
<h2>用<del>矩阵</del>数组进行并行向量计算</h2>
<p>在您的简单示例中,甚至可以使用单个标量矩阵乘法计算所有增量的力:</p>
<pre><code>from numpy import array, linspace
area = 2.0
stresses = array([ linspace(0., 10., inc) for inc in range(1, 4)])
print 'stresses =\n', stresses
forces = area * stresses
print
print 'forces =\n', forces
</code></pre>
<h2>输出</h2>
<pre><code>stresses =
[[ 0.] [ 0. 10.] [ 0. 5. 10.]]
forces =
[[ 0.] [ 0. 20.] [ 0. 10. 20.]]
</code></pre>
<p>现在,由于行的大小不同(每行代表一个增量),<code>stresses</code>并不是一个真正的矩阵,只是一个数组。因此,向量矩阵乘法和矩阵矩阵乘法(这将需要写现实,更复杂的计算,这简洁)将失败。(标量矩阵乘法不关心矩阵的形状。显然,它甚至不必是<a href="https://en.wikipedia.org/wiki/Matrix_%28mathematics%29" rel="nofollow">actual matrix</a>。)</p>
<h2>实矩阵</h2>
<p>但是如果我理解正确的话,尺寸的变化仅仅是因为弹簧在各自的增量中没有应力。你知道吗</p>
<p>因此,如果您想使用矩阵运算,而不是完全保留零项,请考虑使用<a href="http://docs.scipy.org/doc/scipy/reference/sparse.html" rel="nofollow">sparse matrices</a>。它们避免存储零条目或对它们进行计算,这样只会产生新的零条目,但它们仍然“知道”有零条目和矩阵的完整形状(从而保持矩阵乘法等有意义)。你知道吗</p>