集成帮助系统

Sage拥有集成帮助系统。输入函数名加一个问号”?”,可以查看函数相关的文档。

  1. sage: V = QQ^3
  2. sage: V.coordinates?
  3. Type: instancemethod
  4. Base Class: <type 'instancemethod'>
  5. String Form: <bound method FreeModule_ambient_field.coordinates of Vector
  6. space of dimension 3 over Rational Field>
  7. Namespace: Interactive
  8. File: /home/was/s/local/lib/python2.4/site-packages/sage/modules/f
  9. ree_module.py
  10. Definition: V.coordinates(self, v)
  11. Docstring:
  12. Write v in terms of the basis for self.
  13. Returns a list c such that if B is the basis for self, then
  14. sum c_i B_i = v.
  15. If v is not in self, raises an ArithmeticError exception.
  16. EXAMPLES:
  17. sage: M = FreeModule(IntegerRing(), 2); M0,M1=M.gens()
  18. sage: W = M.submodule([M0 + M1, M0 - 2*M1])
  19. sage: W.coordinates(2*M0-M1)
  20. [2, -1]

如上所示,输出结果告诉你对象的类型,在哪个文件中被定义,以及一些有用的描述和例子,例子都可以复制到当前的会话中执行。几乎所有这些例子都经过正规的自动测试,以保证其能够正常运行。

Sage的另外一个非常符合开源精神的功能是,如果f是一个Python函数,那么输入f??就会显示定义f的源码。

  1. sage: V = QQ^3
  2. sage: V.coordinates??
  3. ...
  4. Source:
  5. def coordinates(self, v):
  6. """
  7. Write$v$in terms of the basis for self.
  8. ...
  9. """
  10. return self.coordinate_vector(v).list()

这就告诉我们coordinates所做的工作就是调用coordinate_vector函数并将结果输出到一个列表中。而coordinate_vector做了什么?

  1. sage: V = QQ^3
  2. sage: V.coordinate_vector??
  3. ...
  4. def coordinate_vector(self, v):
  5. ...
  6. return self.ambient_vector_space()(v)

coordinate_vector函数将它的输入代入到环绕空间, 这会影响$V$中$v$的系数向量的计算。 空间$V$已经是环绕的,因为它是$Q^3$。子空间也有coordinate_vector函数,但是是不同的。我们新建一个子空间看看:

  1. sage: V = QQ^3; W = V.span_of_basis([V.0, V.1])
  2. sage: W.coordinate_vector??
  3. ...
  4. def coordinate_vector(self, v):
  5. """
  6. ...
  7. """
  8. # First find the coordinates of v wrt echelon basis.
  9. w = self.echelon_coordinate_vector(v)
  10. # Next use transformation matrix from echelon basis to
  11. # user basis.
  12. T = self.echelon_to_user_matrix()
  13. return T.linear_combination_of_rows(w)

(如果你觉得这个实现不够有效,请帮助优化线性代数。)

你还可以输入help(command_name)help(class)来得到想要的帮助。

  1. sage: help(VectorSpace)
  2. Help on class VectorSpace ...
  3. class VectorSpace(__builtin__.object)
  4. | Create a Vector Space.
  5. |
  6. | To create an ambient space over a field with given dimension
  7. | using the calling syntax ...
  8. :
  9. :

当你输入q以退出帮助系统时,你的会话和进入的时候一样。帮助系统的输出不会像function_name?那样搞乱你的会话。 输入help(module_name)特别有用。例如, 向量空间定义在sage.modules.free_module中, 输入help(sage.modules.free_module)可以查看整个模块的文档。 当使用帮助系统查看文档时,可以输入/进行查找, 或输入?进行反向查找。