notes_September
来源:互联网 发布:数据库简答题 编辑:程序博客网 时间:2024/06/07 09:41
Learning note September
- Learning note September
- Python 矩阵运算和三角函数
- python gzipzlib 模块的使用
- python读文件时的几种权限问题
- gvimvim的使用
- ROS的tf包坐标变换方法
- Python PyKDL模块的用法
- Python reduce模块的使用
- Python filter模块的使用
1. Python 矩阵运算和三角函数
Python的矩阵运算主要通过numpy实现。numpy对矩阵的主要操作如下:
import numpy as npa1 = np.array([1,2]) #1-D arraya2 = np.array([1,2], dtype = float)print a1, a2, a1.dtype, a2.dtype, a1.shape #(2,)
也可以实现二维数组:
a3 = np.array([[1,2],[3,4]], dtype = int)print a3.shape #(2,2)
创建标准矩阵:
b1 = np.zeros((3,2)) #3 columns 2 rows 0 matrixb2 = np.identity(4) #4*4b3 = np.eye(4) #4*4
按规则创建一维数组:
c1 = np.arange(0,1,0.1) #[0., 0.1, ..., 0.9]c2 = np.linspace(0,1,10) #[0., 0.111, ... , 0.888889, 1.]#同理,有logspace()函数
伴随矩阵/逆矩阵/转置/行列式/迹/范数/条件数/特征向量/特征值:
d1 = np.linalg.companion(a3) #companion ?no mudule foundd2 = np.linalg.inv(a3) #inversed3 = a3.T #transposed4 = np.linalg.det(a) #hanglieshid5 = a3.trace() #traced6 = np.linalg.norm(a3, ord=None) #normd7 = np.linalg.cond(a3, p=None) #tiaojianshud8 = np.linalg.eig(a3) #tezheng vector and value
矩阵分解:
常见的矩阵分解函数,numpy.linalg均已经提供。比如cholesky()/qr()/svd()/lu()/schur()等。某些算法为了方便计算或者针对不同的特殊情况,还给出了多种调用形式,以便得到最佳结果。
矩阵运算:
np.dot(a,b)用来计算数组的点积;vdot(a,b)专门计算矢量的点积,和dot()的区别在于对complex数据类型的处理不一样;innner(a,b)用来计算内积;outer(a,b)计算外积。
专门处理矩阵的数学函数在numpy的子包linalg中定义。比如np.linalg.logm(A)计算矩阵A的对数。可见,这个处理和MATLAB是类似的,使用一个m后缀表示是矩阵的运算。在这个空间内可以使用的有cosm()/sinm()/signm()/sqrtm()等。其中常规exp()对应有三种矩阵形式:expm()使用Pade近似算法、expm2()使用特征值分析算法、expm3()使用泰勒级数算法。在numpy中,也有一个计算矩阵的函数:funm(A,func)。
2. python gzip、zlib 模块的使用
!!为了测试gzip模块,我自己写了一个程序就叫gzip.py,后来一直报错!这样命名是不行的,因为import gzip的时候的库文件也叫这个名字,此时会在当前文件夹下先查找,所以就找不到系统的gzip文件了。!!
gzip官方教程
# read a compressed fileimport gzipwith gzip.open('file.txt.gz', 'rb') as f: file_content = f.read()
# create a compressed GZIP fileimport gzipcontent = "lots of content here" #string of file addresswith gzip.open('file.txt.gz', 'wb') as f: f.write(content)
zlib库的一个使用例子
zlib主要是进行数据压缩的。比如把一个字符串压缩成一个更短的字符串,可以反解回来。
3. python读文件时的几种权限问题
#!/usr/bin/python# -*- coding: UTF-8 -*-fo = open("test.txt", "r+b")for i in range(3): str = 'abcd\n' fo.write( str )fo.close()
权限可以有:w, wb, w+b, r, rb, r+b, a, ab, a+b等。b是二进制,w, wb, w+b表示写权限,完全删掉原来的全部内容重新写。r, rb, r+b是读权限。a, ab, a+b是在原来的基础上继续写。最好用带b的。
4. gvim/vim的使用
gvim颜色方案,字体设置等
网上下载字体如何安装
:tabnew等相关操作
5. ROS的tf包坐标变换方法
这篇博客写出了如何利用tf的broadcaster发送四元数或者RPY角度的坐标变换关系。博客链接
transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) );
transform.setRotation( tf::Quaternion(0, 0, 0, 1) );
br.sendTransform(tf::StampedTransform(transform,ros::Time::now(), “turtle1”, “carrot1”));
这里面用的是四元数,如果我们只有旋转矩阵或者RPY角度时,可以进行如下操作:
static tf::TransformBroadcaster br;
tf::Transform transform;
transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );
tf::Quaternion q;
q.setRPY(0, 0, msg->theta);
transform.setRotation(q);
br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), “world”, turtle_name));
默认发出的也是四元数:即使设置是1.57,0,1.57,在发送的时候也会自动转换为四元数,可以通过rostopic echo来查看。
node pkg=”tf” type=”static_transform_publisher” name=”camera_link” args=”0.09 0.03 -0.10 1.57 0 1.57 /base /depth_optical_link 100”
6. Python PyKDL模块的用法
wiki tutorials
official site and git
python documentation
PyKDL是一个神奇的库。里面包含了KDL库与vector,rotation, frame, kinematics, dynamics的相关函数和接口。计算机器人学中的运动学/动力学和坐标变换都是相当的彪悍。甚至可以加载urdf模型计算运动学,调用DH相关接口或者chain接口直接创建机器人模型。这个库主要是面向C++的,通过sig的形式做了相应的python接口。这里先讨论论python中KDL的使用。
to be continue…
7. Python reduce模块的使用
需要一个列表,使用reduce模块可以在定义的函数中迭代执行各个元素。举例来说:
def add(a, b): return a+bprint reduce(add, [1,2,3,4,5,6,7,8,9,10]) #55def cat(a, b): return str(a)+str(b)print reduce(cat, [1,2,3,4,5,6,7,8,9,10]) #12345678910print reduce(add, [1,2,3,4,5,6,7,8,9,10],30) #85 #note: here at most three elements, and the last one must be the data type required
8. Python filter模块的使用
对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回. 比如filter质数:
def fil(x): for i in range(2, abs(x)): if x%i == 0: return False return Trueprint filter(fil, range(10, 100))