我正在通过格雷厄姆的书"On Lisp"工作,并且无法理解第37页的以下示例:
If we de?ne exclaim so that its return value incorporates a quoted list, (defun exclaim (expression) (append expression ’(oh my))) > (exclaim ’(lions and tigers and bears)) (LIONS AND TIGERS AND BEARS OH MY) > (nconc * ’(goodness)) (LIONS AND TIGERS AND BEARS OH MY GOODNESS) could alter the list within the function: > (exclaim ’(fixnums and bignums and floats)) (FIXNUMS AND BIGNUMS AND FLOATS OH MY GOODNESS) To make exclaim proof against such problems, it should be written: (defun exclaim …
在Python中,您的类的方法是否可以__rmul__覆盖另一个类的__mul__方法,而不对另一个类进行更改?
出现这个问题是因为我正在为某种类型的线性运算符编写一个类,并且我希望它能够使用乘法语法来乘以 numpy 数组。这是说明该问题的最小示例:
import numpy as np
class AbstractMatrix(object):
def __init__(self):
self.data = np.array([[1, 2],[3, 4]])
def __mul__(self, other):
return np.dot(self.data, other)
def __rmul__(self, other):
return np.dot(other, self.data)
Run Code Online (Sandbox Code Playgroud)
左乘法效果很好:
In[11]: A = AbstractMatrix()
In[12]: B = np.array([[4, 5],[6, 7]])
In[13]: A*B
Out[13]:
array([[16, 19],
[36, 43]])
Run Code Online (Sandbox Code Playgroud)
但右乘默认为np.ndarrays 版本,它将数组分割并逐个元素执行乘法(这不是我们想要的):
In[14]: B*A
Out[14]:
array([[array([[ 4, 8],
[12, 16]]),
array([[ 5, 10],
[15, 20]])],
[array([[ 6, 12],
[18, 24]]),
array([[ 7, 14],
[21, 28]])]], dtype=object)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我怎样才能让它 …
Common Lisp中将元素操作应用于多维数组的"正确"构造是什么?
以下示例应该有助于说明我正在尝试做的事情:
A)假设我想将数组的每个元素增加一个:
0 1 2 1 2 3
3 4 5 -> 4 5 6
6 7 8 7 8 9
Run Code Online (Sandbox Code Playgroud)
B)假设我想添加2个数组:
1 2 -1 -1 0 1
3 4 + -2 -2 -> 1 2
5 6 -3 -3 2 3
C)假设我想找到几个数组中最大的元素,元素:
max( 0 1 , 4 -1 , 0 0 ) -> 4 1
2 3 0 0 8 1 8 3
基本上我认为我正在寻找某种"arraymap"函数,它可以像这样使用:(arraymap f A1 A2 ... An)其中f将n个参数作为输入,而Ai是相同大小的数组.
在上面的例子中,它将被如下使用:
一个)
(setq M #2A((0 1 …Run Code Online (Sandbox Code Playgroud) 问题:如何有效计算 n 维中两个轴对齐框之间的最小距离?
框格式:框 A 和 B 由它们的最小点和最大点 A_min、A_max、B_min、B_max 给出,每个点都是一个 n 维向量。也就是说,这些框可以在数学上写为以下区间的笛卡尔积:
A = [A_min(1), A_max(1)] x [A_min(2), A_max(2)] x ... x [A_min(n), A_max(n)]
B = [B_min(1), B_max(1)] x [B_min(2), B_max(2)] x ... x [B_min(n), B_max(n)]
注意:注意:我问这个问题,并自己回答,因为即使过了这么多年,这个问题(通常是 n 维形式)似乎也没有出现在 stackoverflow 中。一般而言,在互联网上很难找到这个问题的良好答案。经过谷歌搜索后,我最终不得不自己解决这个问题,并在这里发帖以避免未来的人遇到同样的麻烦。
为什么Python中允许使用嵌套字典,而不允许使用嵌套集?
人们可以嵌套字典并动态更改子字典,如下所示:
In [1]: dict1 = {'x':{'a':1, 'b':2}, 'y':{'c':3}}
In [2]: dict2 = {'x':{'a':1, 'b':2}, 'y':{'c':3}}
In [3]: dict1 == dict2
Out[3]: True
In [4]: dict2['x'] = {'d':4}
In [5]: dict1 == dict2
Out[5]: False
Run Code Online (Sandbox Code Playgroud)
另一方面,如果你试图在一个集合中放置一个集合,你会收到一个错误,说它无法完成,因为集合是一个"不可用的类型":
In [6]: set([set(['a'])])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-8e7d044eec15> in <module>()
----> 1 set([set(['a'])])
TypeError: unhashable type: 'set'
Run Code Online (Sandbox Code Playgroud)
但这也没有意义,因为字典也是不可用的,
In [7]: hash({'a':1})
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-44def9788331> in <module>()
----> 1 hash({'a':1})
TypeError: unhashable type: 'dict'
Run Code Online (Sandbox Code Playgroud)
当然,可以将一个冷冻集放在一个集合中, …
当使用numpy.reshape展平某些numpy数组时,我遇到了看似不一致的结果.有时如果我重塑一个数组,它会返回一个包含一行的2D数组,而如果我先复制数组然后执行完全相同的操作,它将返回一维数组.
这似乎主要发生在将numpy数组与scipy数组合并时,并且当我想稍后将平顶数组乘以矩阵时会产生对齐问题.
例如,请考虑以下代码:
import numpy as np
import scipy.sparse as sps
n = 10
A = np.random.randn(n,n)
I = sps.eye(n)
X = I+A
x1 = np.reshape(X, -1)
x2 = np.reshape(np.copy(X), -1)
print 'x1.shape=', x1.shape
print 'x2.shape=', x2.shape
Run Code Online (Sandbox Code Playgroud)
运行时打印:
x1.shape= (1, 100)
x2.shape= (100,)
Run Code Online (Sandbox Code Playgroud)
numpy.flatten()也会发生同样的事情.这里发生了什么?这种行为是故意的吗?