集成帮助系统
Sage拥有集成帮助系统。输入函数名加一个问号”?”,可以查看函数相关的文档。
sage: V = QQ^3sage: V.coordinates?Type: instancemethodBase Class: <type 'instancemethod'>String Form: <bound method FreeModule_ambient_field.coordinates of Vectorspace of dimension 3 over Rational Field>Namespace: InteractiveFile: /home/was/s/local/lib/python2.4/site-packages/sage/modules/free_module.pyDefinition: V.coordinates(self, v)Docstring:Write v in terms of the basis for self.Returns a list c such that if B is the basis for self, thensum c_i B_i = v.If v is not in self, raises an ArithmeticError exception.EXAMPLES:sage: M = FreeModule(IntegerRing(), 2); M0,M1=M.gens()sage: W = M.submodule([M0 + M1, M0 - 2*M1])sage: W.coordinates(2*M0-M1)[2, -1]
如上所示,输出结果告诉你对象的类型,在哪个文件中被定义,以及一些有用的描述和例子,例子都可以复制到当前的会话中执行。几乎所有这些例子都经过正规的自动测试,以保证其能够正常运行。
Sage的另外一个非常符合开源精神的功能是,如果f是一个Python函数,那么输入f??就会显示定义f的源码。
sage: V = QQ^3sage: V.coordinates??...Source:def coordinates(self, v):"""Write$v$in terms of the basis for self...."""return self.coordinate_vector(v).list()
这就告诉我们coordinates所做的工作就是调用coordinate_vector函数并将结果输出到一个列表中。而coordinate_vector做了什么?
sage: V = QQ^3sage: V.coordinate_vector??...def coordinate_vector(self, v):...return self.ambient_vector_space()(v)
coordinate_vector函数将它的输入代入到环绕空间, 这会影响$V$中$v$的系数向量的计算。 空间$V$已经是环绕的,因为它是$Q^3$。子空间也有coordinate_vector函数,但是是不同的。我们新建一个子空间看看:
sage: V = QQ^3; W = V.span_of_basis([V.0, V.1])sage: W.coordinate_vector??...def coordinate_vector(self, v):"""..."""# First find the coordinates of v wrt echelon basis.w = self.echelon_coordinate_vector(v)# Next use transformation matrix from echelon basis to# user basis.T = self.echelon_to_user_matrix()return T.linear_combination_of_rows(w)
(如果你觉得这个实现不够有效,请帮助优化线性代数。)
你还可以输入help(command_name)或help(class)来得到想要的帮助。
sage: help(VectorSpace)Help on class VectorSpace ...class VectorSpace(__builtin__.object)| Create a Vector Space.|| To create an ambient space over a field with given dimension| using the calling syntax ...::
当你输入q以退出帮助系统时,你的会话和进入的时候一样。帮助系统的输出不会像function_name?那样搞乱你的会话。 输入help(module_name)特别有用。例如, 向量空间定义在sage.modules.free_module中, 输入help(sage.modules.free_module)可以查看整个模块的文档。 当使用帮助系统查看文档时,可以输入/进行查找, 或输入?进行反向查找。
