pyvec
ΒΆ
Commandline vector calculation tool.
- Usage:
% pyvec.py [-h] [-v] [-b] command args
- Global option:
- -b
Only values are shown in the results
- -v
Verbose mode
- -h
Help
Supported calculations are:
- Normalize. Input = (x,y,z) Output = (x0,y0,z0)
% pyvec.py normalize 4 5 3 (0.565685 0.707107 0.424264)
- Outer product. Input = (x0,y0,z0) (x1,y1,z1) Output = (x2,y2,z2)
% pyvec.py outer 1 3 2 -1 -2 4 (16.000000 -6.000000 1.000000)
- Inner product. Input = (x0, y0, z0) (x1, y1, z1) Output = z
% pyvec.py inner 1 3 2 -1 -2 4 1
- Length and lengthSquared. Input = (x0, y0, z0) Output = L or L^2
% pyvec.py length 1 3 2 sqrt(14) % pyvec.py lengthSquared 1 3 2 14
- Vector to polar angles. Input = (x,y,z) Output = (r, theta, phi)
% pyvec.py topolar 3 5 -1 (r=5.916080 t=99.731475 p=59.036243)
- Polar angles to vector. Input = (r, theta, phi) Output=(x,y,z)
% pyvec.py tovector 5 30 -45 (x=1.767767 y=-1.767767 z=4.330127)
#!/usr/bin/env python
"""
Commandline vector calculation tool.
Usage:
% pyvec.py [-h] [-v] [-b] command args
Global option:
-b Only values are shown in the results
-v Verbose mode
-h Help
Supported calculations are:
- Normalize. Input = (x,y,z) Output = (x0,y0,z0)
% pyvec.py normalize 4 5 3
(0.565685 0.707107 0.424264)
- Outer product. Input = (x0,y0,z0) (x1,y1,z1) Output = (x2,y2,z2)
% pyvec.py outer 1 3 2 -1 -2 4
(16.000000 -6.000000 1.000000)
- Inner product. Input = (x0, y0, z0) (x1, y1, z1) Output = z
% pyvec.py inner 1 3 2 -1 -2 4
1
- Length and lengthSquared. Input = (x0, y0, z0) Output = L or L^2
% pyvec.py length 1 3 2
sqrt(14)
% pyvec.py lengthSquared 1 3 2
14
- Vector to polar angles. Input = (x,y,z) Output = (r, theta, phi)
% pyvec.py topolar 3 5 -1
(r=5.916080 t=99.731475 p=59.036243)
- Polar angles to vector. Input = (r, theta, phi) Output=(x,y,z)
% pyvec.py tovector 5 30 -45
(x=1.767767 y=-1.767767 z=4.330127)
"""
import sys,os
import getopt
from irfpy.util import *
import math
import logging
def usage():
print(__doc__)
if __name__ == '__main__':
simpleOut=False
try:
optlist, arglist=getopt.getopt(sys.argv[1:], 'vhb')
except Exception as e:
usage()
sys.exit(-1)
for opt,optarg in optlist:
if opt in ('-v'):
logging.basicConfig(level=logging.DEBUG)
logging.debug('Verbose mode ON')
if opt in ('-b'):
logging.debug('-b is set')
simpleOut=True
if opt in ('-h'):
usage()
sys.exit(0)
if len(arglist) == 0:
usage()
logging.error('Command needed.')
sys.exit(-2)
cmd=arglist[0]
cmdarg=arglist[1:]
if cmd in ('normalize'):
logging.info(cmd)
if len(cmdarg) != 3:
print('USAGE: pyvec.py %s x y z'%cmd, file=sys.stderr)
sys.exit(-5)
x0=float(cmdarg[0])
y0=float(cmdarg[1])
z0=float(cmdarg[2])
nm=norm.norm((x0,y0,z0))
if simpleOut:
print('%f %f %f'%(nm[0],nm[1],nm[2]))
else:
print('(%f %f %f) \'s normal vecotr\n=\n(%f %f %f)'%(x0,y0,z0, nm[0],nm[1],nm[2]))
elif cmd in ('outer'):
logging.info(cmd)
if len(cmdarg) != 6:
print('USAGE: pyvec.py %s x0 y0 z0 x1 y1 z1'%cmd, file=sys.stderr)
sys.exit(-5)
x0=float(cmdarg[0])
y0=float(cmdarg[1])
z0=float(cmdarg[2])
x1=float(cmdarg[3])
y1=float(cmdarg[4])
z1=float(cmdarg[5])
op=outer.outer((x0,y0,z0), (x1,y1,z1))
if simpleOut:
print('%f %f %f'%(op[0],op[1],op[2]))
else:
print('(%f %f %f) x (%f %f %f)\n=\n(%f %f %f)'%(x0,y0,z0, x1,y1,z1, op[0],op[1],op[2]))
elif cmd in ('inner'):
logging.info(cmd)
if len(cmdarg) != 6:
print('USAGE: pyvec.py %s x0 y0 z0 x1 y1 z1'%cmd, file=sys.stderr)
sys.exit(-5)
x0=float(cmdarg[0])
y0=float(cmdarg[1])
z0=float(cmdarg[2])
x1=float(cmdarg[3])
y1=float(cmdarg[4])
z1=float(cmdarg[5])
op = x0 * x1 + y0 * y1 + z0 * z1
if simpleOut:
print('%f' % op)
else:
print('(%f %f %f) . (%f %f %f)\n=\n%f'%(x0, y0, z0, x1, y1, z1, op))
elif cmd in ('topolar'):
if len(cmdarg) != 3:
print('USAGE: pyvec.py %s x y z'%cmd, file=sys.stderr)
sys.exit(-5)
x0=float(cmdarg[0])
y0=float(cmdarg[1])
z0=float(cmdarg[2])
nm=v3ang.v3ang((x0,y0,z0))
if simpleOut:
print('%f %f %f'%(nm[0],nm[1],nm[2]))
else:
print('(%f %f %f) \'s polar expression\n=\n(r=%f t=%f p=%f)'%(x0,y0,z0, nm[0],nm[1],nm[2]))
elif cmd in ('length'):
if len(cmdarg) != 3:
print('USAGE: pyvec.py %s x y z'%cmd, file=sys.stderr)
sys.exit(-5)
x0=float(cmdarg[0])
y0=float(cmdarg[1])
z0=float(cmdarg[2])
l = math.sqrt(x0*x0 + y0*y0 + z0*z0)
if simpleOut:
print('%f' % l)
else:
print('len(%f %f %f)\n=\n%f'%(x0,y0,z0, l))
elif cmd in ('lengthSquared'):
if len(cmdarg) != 3:
print('USAGE: pyvec.py %s x y z'%cmd, file=sys.stderr)
sys.exit(-5)
x0=float(cmdarg[0])
y0=float(cmdarg[1])
z0=float(cmdarg[2])
l = x0*x0 + y0*y0 + z0*z0
if simpleOut:
print('%f' % l)
else:
print('len^2(%f %f %f)\n=\n%f'%(x0,y0,z0, l))
elif cmd in ('tovector'):
if len(cmdarg) != 3:
print('USAGE: pyvec.py %s r theta[deg] phi[deg]'%cmd, file=sys.stderr)
sys.exit(-5)
x0=float(cmdarg[0])
y0=float(cmdarg[1])
z0=float(cmdarg[2])
nm=vfang.vfang(x0,y0,z0)
if simpleOut:
print('%f %f %f'%(nm[0],nm[1],nm[2]))
else:
print('(r=%f t=%f p=%f)]\npolar expression is the vector\n=\n(x=%f y=%f z=%f)'%(x0,y0,z0, nm[0],nm[1],nm[2]))
else:
usage()
print('Command not found: %s'%cmd, file=sys.stderr)
sys.exit(-3)