Tuesday, December 6, 2016

The opensource alternatives for codegen of Mupad, codegeneration of Maple (sympy)

Example code for getting optimized(CSE) code of jacobian from symbolic calculation
from sympy import Matrix, sin, cos, cse
from sympy.abc import rho, phi
from sympy.utilities.codegen import codegen
from sympy.printing import print_ccode

X, Y = Matrix([rho*cos(phi),rho*sin(phi),rho**2]), Matrix([rho, phi])
J = X.jacobian(Y)
tmp, result = cse(J)

for l in tmp:
    l = 'double %s = %s;'%(l[0], l[1])
    print l
result = result[0].tolist()
for r, row in enumerate(result):
    for c, elem in enumerate(row):
        l = 'output[%d][%d] = %s;'%(r,c,elem)
        print l
the result will be

double x0 = cos(phi);
double x1 = sin(phi);
output[0][0] = x0;
output[0][1] = -rho*x1;
output[1][0] = x1;
output[1][1] = rho*x0;
output[2][0] = 2*rho;
output[2][1] = 0;

Sunday, November 6, 2016

ros pycharm

ref1: http://wiki.ros.org/IDEs#PyCharm_.28community_edition.29
ref2: http://stackoverflow.com/questions/19738980/recreating-pycharm-launcher-in-ubuntu

vim ~/.local/share/applications/jetbrains-pycharm.desktop 에서

Exec="/opt/pycharm-community-4.0.4/bin/pycharm.sh" %f
에 해당하는 문장을,

Exec=bash -i -c "/opt/pycharm-community-4.0.4/bin/pycharm.sh" %f
으로 수정

Thursday, November 3, 2016

roslaunch debugging

http://answers.ros.org/question/47210/how-to-roslaunch-node-in-gdb/
http://wiki.ros.org/roslaunch/Tutorials/Roslaunch Nodes in Valgrind or GDB

http://www.ceh-photo.de/blog/?p=899

[작성예정] gdb, 코어덤프

코어덤프를 얻었을 때의 최대 이점은 문제가 발생했을 때의 상태를 보존할 수 있다는 점이다. 문제가 발생한 프로그램의 실행파일과 코어덤프가 있다면 당시 프로세스의 상태를 알 수 있다.
 - Debug hacks, 요시오카 히로타카, 2010.

Thursday, September 15, 2016

boost-python wrapping. accessing to numpy array example

This is a example for boost-python, especially about numpy array.
The codes are about CMakeLists.txt, hello.py, hello.cpp file.
 --------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
find_package(PythonLibs REQUIRED)
string(REGEX MATCH "([0-9])\\.*" output ${PYTHONLIBS_VERSION_STRING})
if(${output} EQUAL "2.")
 find_package(Boost COMPONENTS python)
else()
    find_package(Boost COMPONENTS python3)
endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR})
file(GLOB lib_headers "src/*.h")
file(GLOB lib_src "src/*.cpp")
set(prjname hello)
add_library(${prjname} SHARED ${lib_src} ${lib_headers})
set_target_properties(${prjname} PROPERTIES DEBUG_POSTFIX "_d")
if(WIN32)
  set_target_properties(${prjname} PROPERTIES SUFFIX ".pyd")
endif(WIN32)
target_include_directories(${prjname} PUBLIC ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
target_link_libraries(${prjname} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
--------------------------------------------------------------------------
 
if __debug__:
 import hello_d as hello
else:
 import hello
import numpy as np

hello.greet()
uv = np.array([[1.0, 2.0]])
print 'uv =', uv
hello.get_set_element(uv, 3.0)
print 'uv0 = ', uv

#include <boost/python.hpp>
#include <boost/python/numeric.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/tuple.hpp>

#ifdef _DEBUG
#define PY_MODULE BOOST_PYTHON_MODULE(hello_d)
#else
#define PY_MODULE BOOST_PYTHON_MODULE(hello)
#endif

char const* greet(){
        return "hello\n";
}

void get_set_element(boost::python::numeric::array& y, double value){
        double d = boost::python::extract<double>(y[boost::python::make_tuple(0, 0)]);
        printf("array[0,0] = %f\n", d);
        y[boost::python::make_tuple(0, 0)] = value;
}

PY_MODULE{
        boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
        boost::python::def("get_set_element", &get_set_element);
        boost::python::def("greet", greet);
}

Sunday, July 3, 2016

Parallel programming with python

Module multiprocessing vs pp (parallel python) :
http://stackoverflow.com/questions/5181949/using-the-multiprocessing-module-for-cluster-computing

multiprocessing looks like better, general, if multiple node doesn't necessary.

multiprocessing
https://docs.python.org/2/library/multiprocessing.html

List of the libraries :
https://wiki.python.org/moin/ParallelProcessing

example)
http://www.parallelpython.com/content/view/17/31
http://www.parallelpython.com/content/view/15/30/#QUICKSMP
http://www.parallelpython.com/content/view/15/30/#QUICKCLUSTERS
To launch ppserver,
$ python /usr/local/bin/ppserver.py -s mypassword

#myclient.py
import pp
server = pp.Server(ppservers=("127.0.0.1", secret='mypassword')

Monday, March 28, 2016