Nugroho's blog.

Sunday, December 11, 2011

The Journey of Installing Matplotlib Python Module on OS X Lion

I need matplotlib to plot my python output when I am running my output function generator python code.

This post is a log of what I did to being able to install matplotlib 1.1.0 on python 2.7.2 on my Mac OS X Lion 10.7.2. Yet, it's unfinished job.


First, googling for matplotlib, sourceforge is official home fon it, but it's very slow, I coudn't even open download page with my sluggish connection. So, I searching other source.

Got it from kambing.ui.ac.id, it has pypi repositories, but when it opened, there is no package, just blank folder.

After further googling, I finally found http://pypi.python.org, pypi stand for python package index.

To be able to use pypi package, we have to install pip first, but before that install distribute using this command

curl http://python-distribute.org/distribute_setup.py | python
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

and then install matplotlib using pip, there is download activity, but got error at the end; must install numpy first, but it got error too as I didn't have GCC on my lion yet, aaarrrgghh... So, the hell of dependencies is begin...

So here I am, searching for 'light' GCC for my lion. I know I should install XCode 4 from Apple , it's free anyway, but I must face the fact that it's including 4.5GB download job, such a tedious job and wasting time; I just want to install 13 MB matplotlib.

I wish I can type the code below

pip install numpy
pip install matplotlib

(pray)

Saturday, December 10, 2011

Portable Python

When I wandering around, through virtual world, looking for Python reference of matplotlib, don't know what link I'd click, suddenly I'm landing in Portable Python page. Barely interested, not because it's not interesting, but I've already have python on my Mac and this Portable Python came with .exe download, such a tedious job if I try to run it on my machine (clearly, it's Windows apps). However, it's really useful distribution of Python.



At download section, it's recommended to download via torrent network, another interesting idea. Here some excerpt from Portable Python website



Portable Python is a Python® programming language preconfigured to run directly from any USB storage device, enabling you to have, at any time, a portable programming environment. Just download it, extract to your portable storage device or hard drive and in 10 minutes you are ready to create your next Python® application.

One of the most powerful dynamic programming languages that is used in a wide variety of application domains and is used at many companies and institutions around the world ( YouTube, Google, NASA, Firaxis Games, etc.).

iRig

Several weeks ago my iRig package has come, finally. I expect it'll come earlier but alas my home's in the middle of nowhere surounded with forest and mountain, even google map refuse to map my place, :). At least it comes safely at my front door.

 I planned to use it with Amplitube on my iPad. It worked flawlessy of course. A minor annoyance, it's built in jack is undetachable, so it's uncool if you planned to bring it on your front pocket.

 I'm surprised when the thing work well with my macbookpro too. My 13' MacBook Pro only have one port audio, either for input or output, so it's big 'yeah yeah' for me for be able to record my guitar on GarageBand AND listen it.

 iRig work with GarageBand for iPad too, and several music apps like digital effect apps for ipad (forget its name), I'm sure not just that. Sadly, amplitube didn't support more space for effect, eight slot effect will cool. Here some screenshot.

From Blogsy Photos
From Blogsy Photos
From Blogsy Photos
From Blogsy Photos

Script Python untuk Menghitung Nilai Input berupa Fungsi

Script di bawah adalah kode python sederhana untuk menghitung nilai sebuah fungsi yang dimasukkan sebagai input pada variabel tertentu. Fungsi yang diinputkan bisa bermacam-macam.



Kita bisa memasukkan fungsi kuadrat, sinus, pangkat tiga dan lain-lain sebagai input.

import sys,parser
from math import *
y = sys.argv[1]
x = int(sys.argv[2])
z = parser.expr(y).compile()
print 'Nilai fungsi ', y, ' pada x = ',x,' adalah ',eval(z)


Simpan dengan nama f.py. Jalankan dengan perintah

python f.py

Berikut beberapa hasilnya, lengkap beserta kesalahan-kesalahannya

Nugrohos-MacBook-Pro:python nugroho$ python f.py x**2 4
Nilai fungsi  x**2  pada x =  4  adalah  16
Nugrohos-MacBook-Pro:python nugroho$ python f.py sin(x) 4
-bash: syntax error near unexpected token `('
Nugrohos-MacBook-Pro:python nugroho$ python f.py 'sin(x)' 4
Nilai fungsi  sin(x)  pada x =  4  adalah  -0.756802495308
Nugrohos-MacBook-Pro:python nugroho$ python f.py 'sin(x)+x**2' 4
Nilai fungsi  sin(x)+x**2  pada x =  4  adalah  15.2431975047
Nugrohos-MacBook-Pro:python nugroho$ python f.py 'x**2+2x-8' 4
Traceback (most recent call last):
  File "f.py", line 5, in
    z = parser.expr(y).compile()
  File "", line 1
    x**2+2x-8
          ^
SyntaxError: invalid syntax
Nugrohos-MacBook-Pro:python nugroho$ python f.py 'x**2-2x-8' 4
Traceback (most recent call last):
  File "f.py", line 5, in
    z = parser.expr(y).compile()
  File "", line 1
    x**2-2x-8
          ^
SyntaxError: invalid syntax
Nugrohos-MacBook-Pro:python nugroho$ python f.py 'x**2-2*x-8' 4
Nilai fungsi  x**2-2*x-8  pada x =  4  adalah  0
Nugrohos-MacBook-Pro:python nugroho$ python f.py 'x**2+2*x-8' 4
Nilai fungsi  x**2+2*x-8  pada x =  4  adalah  16
Nugrohos-MacBook-Pro:python nugroho$ python f.py x**2+2*x-8 4
Nilai fungsi  x**2+2*x-8  pada x =  4  adalah  16
Nugrohos-MacBook-Pro:python nugroho$ python f.py "(x**2+2*x-8+sin(x))/(2*x+2)" 4
Nilai fungsi  (x**2+2*x-8+sin(x))/(2*x+2)  pada x =  4  adalah  1.52431975047
Nugrohos-MacBook-Pro:python nugroho$

Perhatikan bahwa lebih aman untuk menuliskan fungsi di dalam dua tanda petik (bisa petik satu ataupun petik dua).


Agar Python Lebih Manusiawi


Kita dapat menggunakan parser pada python untuk memasukkan input berupa fungsi atau persamaan. Namun, fungsi yang kita masukkan harus mengikuti aturan python, misal kita ingin fungsi y=x^2+2x+2, maka untuk input kita harus memasukkan  x**2+2*x. Memang tidak begitu merepotkan, namun akan lebih baik jika input yang kita masukkan sesuai dengan kebiasaan kita.


Untuk itu kita dapat menambahkan fungsi untuk mengubah x^2 menjadi x**2. Berikut adalah kode untuk melakukannya

>>> w='x^2'
>>> w.replace('^','**')
'x**2' 
>>> w
'x^2'
>>>

Hati-hati bahwa sintaks tersebut tidak benar-benar mengubah variabel w,dia tetap bernilai 'x^2' dan tidak dapat diproses. Untuk dapat mengubah string, maka kita perlu variabel baru untuk menampung dengan perintah y=w.replace('^','**'), atau tampung ke variabel itu sendiri dengan perintah w=w.replace('^','**'). Berikut adalah contohnya

>>> w='x^2'
>>> w
'x^2'
>>> w.replace('^','**')
'x**2'
>>> w
'x^2'
>>> y=w.replace('^','**')
>>> y
'x**2'
>>> w
'x^2'
>>> w=w.replace('^','**')
>>> w
'x**2'
>>> 

Dengan demikian pengguna dapat memberi input berupa x^2 atau x**2 untuk x pangkat dua.

Selain memakai perintah replace, kita juga bisa menggunakan regular expression menggunakan library re.
Berikut adalah hasil coba-coba menggunakan re.

Nugrohos-MacBook-Pro:~ nugroho$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s='persamaan pangkat 2''
  File "", line 1
    s='persamaan pangkat 2''
                           ^
SyntaxError: EOL while scanning string literal
>>> s='persamaan pangkat 2'
>>> s
'persamaan pangkat 2'
>>> import re
>>> re.sub("a",',',s)
'pers,m,,n p,ngk,t 2'
>>> s='persamaan pangkat 2'
>>> re.sub("\a",',',s)
'persamaan pangkat 2'
>>> s='persamaan pangkat 2'
>>> re.sub("\a",' ',s)
'persamaan pangkat 2'
>>> re.sub("a",' ',s)
'pers m  n p ngk t 2'
>>> s='persamaan pangkat 2'
>>> re.sub("a",'',s)
'persmn pngkt 2'
>>> s.replace('p','n')
'nersamaan nangkat 2'
>>> exit

Friday, December 9, 2011

Input berupa Fungsi Fleksibel pada Python dengan Menggunakan Parser


Saat kita membuat sebuah aplikasi, sering kita memberi kesempatan pengguna untuk memberikan input. Misal pada program untuk menghitung akar persamaan kuadarat ax^2+bx+c, kita memberi input berupa nilai a, b dan c. Ini berarti program yang dibuat hanya dapat menyelesaikan persamaan kuadarat dengan model ax^2+bx+c. Bentuk penulisan seperti ini disebut hardcode. Bagaimana misal jika kita menginginkan akar 3x^3-3? Atau menemukan nilai y=sin(x)? Tentu saja kita harus membuat program yang baru.



Pada python ada fungsi parser yang memungkinkan kita untuk memasukkan input berupa persamaan. Dengan demikian kita dapat membuat hanya satu program untuk misal menggambar grafik suatu fungsi dengan input berupa fungsi. Kita bebas memasukkan sebarang persamaan sebagai input.

Berikut adalah contoh program untuk menentukan nilai fungsi pada sebuah peubah. Dalam hal ini fungsi yang diinputkan adalah f=sin(x)*2x^2. Program mencari nilai fungsi tersebut pada x=10.

Nugrohos-MacBook-Pro:~ nugroho$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import sin
>>> import parser
>>> f="sin(x)*2*x**2"
>>> print f
sin(x)*2*x**2
>>> y=parser.expr(f).compile()
>>> x=10
>>> print eval(y)
-108.804222178
>>>


CUDA



Do you have a computer with NVIDIA Graphics Card. If by any chance the answer is yes then you probably can use that machine to do some cool paralel computation task in this area:
Computational Structural Mechanics
Bio-Informatics and Life Sciences
Medical Imaging
Weather and Space
Data Mining and Analytics
Imaging and Computer Vision
Computational Finance
Computational Fluid Dynamics
Electromagnetics and Electrodynamics
Molecular Dynamics


Yeah, if you bought NVIDIA powered machine this year or last year, your graphics card maybe supported paralel computing using CUDA.

CUDA™ is a parallel computing platform and programming model invented by NVIDIA. It enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU).

With millions of CUDA-enabled GPUs sold to date, software developers, scientists and researchers are finding broad-ranging uses for CUDA, including image and video processing, computational biology and chemistry, fluid dynamics simulation, CT image reconstruction, seismic analysis, ray tracing, and much more.

GPU Computing: The Revolution

It's hard to believe that twenty years ago we stuck on a machine with no GUI and no multitasking, at least multitasking is rare thing. And ten years ago we stop increasing speed of processor at 3GHz, more than that is either we burn our computer or move our PC using towing car as conductor can't be any smaller. Developer focused on multicore machine and cluster machine.

You're faced with imperatives: Improve performance. Solve a problem more quickly. Parallel processing would be faster, but the learning curve is steep – isn't it?

Not anymore. With CUDA, you can send C, C++ and Fortran code straight to GPU, no assembly language required.

GPU computing is possible because today's GPU does much more than render graphics: It sizzles with a teraflop of floating point performance and crunches application tasks designed for anything from finance to medicine.

History of GPU Computing

http://www.nvidia.com/object/cuda_home_new.html

The first GPUs were designed as graphics accelerators, supporting only specific fixed-function pipelines. Starting in the late 1990s, the hardware became increasingly programmable, culminating in NVIDIA's first GPU in 1999. Less than a year after NVIDIA coined the term GPU, artists and game developers weren't the only ones doing ground-breaking work with the technology: Researchers were tapping its excellent floating point performance. The General Purpose GPU (GPGPU) movement had dawned.

But GPGPU was far from easy back then, even for those who knew graphics programming languages such as OpenGL. Developers had to map scientific calculations onto problems that could be represented by triangles and polygons. GPGPU was practically off-limits to those who hadn't memorized the latest graphics APIs until a group of Stanford University researchers set out to reimagine the GPU as a "streaming coprocessor."

In 2003, a team of researchers led by Ian Buck unveiled Brook, the first widely adopted programming model to extend C with data-parallel constructs. Using concepts such as streams, kernels and reduction operators, the Brook compiler and runtime system exposed the GPU as a general-purpose processor in a high-level language. Most importantly, Brook programs were not only easier to write than hand-tuned GPU code, they were seven times faster than similar existing code.

NVIDIA knew that blazingly fast hardware had to be coupled with intuitive software and hardware tools, and invited Ian Buck to join the company and start evolving a solution to seamlessly run C on the GPU. Putting the software and hardware together, NVIDIA unveiled CUDA in 2006, the world's first solution for general-computing on GPUs.

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)