Showing posts with label Madoka. Show all posts
Showing posts with label Madoka. Show all posts

Monday, January 2, 2017

Ruby, Metaprogramming, and LinearEquation Solver


Metaprogramming
Metaprogramming is a programming technique in which computer programs have the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running. ~ Wikipedia.
In Ruby, there's an enormous amount of function, that would help you writing a program, that could dynamically modify itself.

Most common example is:
  • define_method(methodname, &block)

    define_method is a method that take two argument, a symbol or string as the methodname, and the block process of what it should do.

    for example, if I have a class, that refer to a user table in mysql with field name, email, and password, i can define 3 new method dynamically, namely find_by_name, find_by_email, and find_by_password with the following code: (not accurate, but you get the point)


  • method_missing?(methodname)

    method missing is a method that just accept a string or symbol argument, this is a method that allows you to handle any arbitrary method when it is called.
    Suppose your User have a field name, email, and password, and then there's user info where we put his address, facebook_id, phone_number, and so on. Now you might want to write your program so that a User object may receive any method in UserInfo, well, it'll probably be written like:

Alright, so we've seen how we can write a program that could modify itself mid-runtime, or defining what is not defined. Since we've seen how we write a program that "write" itself, it's also possible for our program to read its own source-code.

Meet the "source-code".

In the next example, We'll look how we implement a linear-equation solver using metaprogramming, (not really useful since then, our equation is hardcoded, but why not?).

Look at the following example of how we define our equation:

Guess what, there's no variable x, y, or z in program, we also didn't create new behavior for Fixnum instance by defining .x, .y, or .z method, and of course we doesn't use method_missing? as well.
Would it then throw error? well, no, not until it's evaluated.

If the codes will be an error when it's evaluated, then just don't evaluate it ~ Ramadoka:2017 


 Which is exactly what I'll do.

Let's take a look at what it does

what magic did we found there?

you might guessed it correctly, it's the @eq.source 

that magic, allows us to read the source-code of our function, and since ruby is dynamic-typed interpreted language, it doesn't know whether an object would respond to a method or not, until it is evaluated, so yeah, there won't be a compile time error, as long as our-code passed the lexical analysis.

Now, that we have done it, we basically just said:

"ruby, kindly, don't parse my code, let me handle that part myself."

Which is pretty meta for my taste...

That marks the end of our topic about metaprogramming, but if you're curious about how the code of LinearEquation works, please feel free to continue along.

Linear Equation Solver

The first step is defining the requirement (can be adjusted later), how do we want to say 3x + 2y + z = 10
since apparently 3x + 2y + z = 10 doesn't pass the lexical analysis (or in other words, ruby know it's not a valid program even without running it).

In this example I'm defining my requirement would be:
3.x => 3x
- x => - x
+ 2 = 10 => not valid (too lazy to reduce to minimum state)
+ z = 10 => + z = 10
+ -x => apparently work as intended
- -x => not working correctly.

If the code doesn't match the requirement, you can either
a) change the code so it meet the requirement
b) change the requirement so it match the code

So Yeah, since this is just a proof of concept, I'm picking the 2nd option.

How do we handle the parsing?

Regex, Regex is always the answer, as anonymous programmer said:

A programmer has a problem, and then he said, I know, I'll use regex.
Now he has 2 problem.

Ok, as you've seen regex is not the silver bullet to handle parsing, we probably need a full-blown parser, but implementing a parser might take 10x time longer than it is, so let's live with the bug now.

as buggy as it is, this regex handles the filtering of a parameter for our basic necessity:
/[+-]* *(?:\d+\.){0,1}[a-zA-Z]+/
by using string.scan using this regex, a 3.x + y + 2.z
will become ["3.x", "+ y", "+2.z"]

and then, there's two other regex to parse our parameters, it can be either:
/(?<multiplier>\d+)\.(?<name>\w+)/
if it start with the multiplier (e.g: 2.x)

or, it can also be:
/(?<multiplier>)(?<name>[a-z]+)/)
if it doesn't have multiplier (e.g: y)

once we have parsed 3.x - 2.y + z = 10 into
[3x, -2y, 1z] and [10]

We will use Matrix Multiplication to solve the equation:

as it turns out, our equation can be rewritten as Matrix Multiplication as: 

Aaaand, that's it, I'm out of material. Thanks for reading this scientific nonsense.

For the full version of the LinearEquation Implementation you can see it here.

Does this post lacking madokaism?, No Longer!
lahirnya juru selamat

Saturday, December 8, 2012

Texified Madoka

                                                                          l7lql          l7lll
                                                                          77Qlql        l7l Ql
                                                                         Q7l  lqV      V7l   l
              VlQ                         UVVqqqqqqqV QVVVppVVV          V7l   lqQ     77    lV
              77mQ                    Vq.........................qqV     l7Q    ml    V7V    ll
             Q7lQlV               Vq.................................qV  q7Q    Rql   lq     Vq
             Q7Q  lR           Vq.......................................l7q      VqV  ml      mR
             V7Q   mQ        V..........................................77qV      lqVQqV      ll
VllQ         l7V   QqQ     p............................................77q..p     VqllQ      Vl
77lqlQ       l7l    Vl   q..............................................77q....V    Qlll Vl  VlV
73VlqqllQQ   V7l     qqq................................................777.....q   V7..q..q7...V
72  Qlmqmq..7777qqq.....................................................777.......Vq.....q..7....V
7l    Qlmll7............q...............................................qq[...............7.7[....V
7l      Vmllllq.......qq................................................ll.......7........q..q.....
7l       QlmlVVlllllllq................................................7ll.................7.7......
7l        Qlmllll..7lq.................................................7ll......7..........q..7.....
7l        7qlllll7.q...................................................7ll[....[7...........[.[.....
77       ...7llllpq....................................................[llq....[............q.......
77      .....7lllV[.........................................[...........qll7...7............[.......
77Q    .......7ll7...................................[......7..........77lll...7.............7......
77V   q........q7.................................77.7......77..........7qllq.77.............[......
77V  V.........q......................7...........77.7[..7..77.[.........7lll777.7..................
73V  .........q......................[77..........77.77..7..777[...7.....7llmll[77..................
7mV q........7[................77....[l7..........77.77..77.[7777..7......7llll.77..[...............
lmlQ.........7.................77....7Q7.........[77.7q..77[77777[.77.[[.[.7llq.77..7...............
Qmll........7....[[............7[....pV7.........[77.7Q7[.77777777[77[777[.77777l7.77...............
 lml........7....7............[7....7llq..[......777[7Ql[.77777777[[7[7777777777l7.77[..............
 Qmlq......7....77............7q....7Q7l777...[..77777lQ7877777l7777777777777777l7.777..............
  lll7.....7....77............77....lQ[p777...[..77q77lUl7.7777l777.77777777777ql7.777.........7....
 77lll....77...[7[...[.......777...7Ul[7777...[..77l77lpQ777777ll77.77777[[7777lq7.777.........7[...
q..qll7...7....77...[.....[..7q7...7Q7[7777...7..7ql77l7Ul7.777ll77.77778.77777lp7.777.........77...
...[lll7.[7.[.[77...[.......77l7...ll[[7777...7..7ll77l[ll7.777lV777.7777.[[7777l7.7777........77...
....7lll777.7.7l7..7[.......77V7..7ll[.[p777..7..7ll77l[7lq7777qQl77.777[...7l77ll77777.7.7[..[77...
.....7lllq[77[7q7..7........77V7..7U7[..7777..7..7l777l.77l7..77Ul77.77[[...7l77ll777q777.77.[777..[
7.....7ll7.7.777..[7...[....77l7..7l[[..7777..7..7l777l[[777..[7ll77.77[[...l777lq777q777.77..777..[
7[...7..777777q7[.77.....[..lql7..lp[[..[p77..7.87l777p[[lpl7777ll77.7q[....l777ll777l7[7.77.[777[.[
.7..[77.777777l7..7........7lql7.7lp7qq..pq7..7.77l[777[.[7p777llpq7..l7...7l7777ll77l777.77.7777[77
7[..77.7777777l7..7........7llp77ql77[...7l77.7.77l[777[..777.77lql77.l7...777777ll77l7[7.77..777777
7..777.777777ll7.7[........qllp777l[[....[l77.7.77l[777[..77p787p77lq7q7...q777777q77l777..7.[[77777
..777..777777Rq7.7....7....llll7.77[[.....p77.7.77p[777[..[7p7.7p7777ql7...7.77777777l7.7..7.7[77777
[7777..77777ll7.77....7....lll77777[[.....7q7.7.77p[7p[[...[7777777677l7..77.7777l7l7l777..7.7777777
77q7..77777lll7.77..[.7...7llq77777[[......p77777p7[7p[[....[p7777[677ql..7..7777l777l7777.7.7777777
7777..77777l7l7.7...[.7...7ll77777[[[......777777l7[76[[.....77777[77777777..7777l777lm777[7.7777777
7q77..7777l77l7.7..7..7..7qll77777[[[......[77777l7.[7[.....[7p777[77777.q...7777l77qll777[7.7777777
q77...777ql77l7[7..7..7.777lp7777[.[[.......777777[.[q[..pQ  QQVl7.[7777.7...7777l77l7l777[7.77q7[77
V77..7777l77lq.7[.7..77.777lp7777..7lQQ p...777777[.[q...V Q     Qp[77l7.....7777l77l7l777[7.77q7777
Q7...777l777lq.7..7..7777[7lp77777l     V....q7777[.[7...qq.qV     Q77l7.....7777777m7l77777.77q7777
l7...77ml777l7.7.77..777.77l6777l    QQq7....q7777...[........VQQQ  Qll7.....7777777q7l77777.77l7777
q7..777l7777l777.77..777777q77[l  QQQQQl.....[777[............VQQQQQ  l7.....777777qqqlq7777.77l7777
l...77l77777l777.77..777777p7[p  qVQQQQQ......q77............7QQQQQQQ Q7.....7777ll7..ql7777777l7777
l..77ll77777l777777..777777l[7  q.l QQQQq.....7p[...........mVQQQQQQVQ l.....7777l..[.7l7777777l7777
q..7ql777777l777777..777777l[Q l..VQQQQQl......p............QQQQQQQQV7 Q.....77777.qp7[l777777qq7777
[.[ql7777.77q77777l..777777pq VqllQQQQQVV......[............QQQQQQQVV7Q 7....77777l7[7[p777777q77777
..7q77777.7777777lV7.777777qU 7q QQQQQRlV..................7QQQQQQRlQ7l l...77777ll77[[p777777q77777
..q77777..7777777VQ7.77ll77l U.7QQQQQQllQ..................qVllQQQlqQ77QQ...77777777[[[pp77777l77777
..7.77q7..7777777Q q.77ll77l l.7QlVQQQlqQ..................qVqlQVVlqQ77VQ7..77777[77[.[qq77777l77777
...777q7..777777q  q.77lU77l q.7VllQVQlpU..................7Q7qlll77Q777V7777777p7[[77.7777777lp7777
...777l7..777777l  l.77lVp7l 7.7V77lQl7lV................... q.....lQ.77l.777777l7[[77.77q7777ll7777
...77lQ77.777777V  V777VQp7pl...Q777q7.Vl...................l q..[l q.7qq.777777l7[[p[.q.q777qQl7777
...77V 77.77q777l  Q777lQ777q...VV....7 7....................l  Q  V.77l7777777ql777q.7q.ll77lQV7777
..77q  q7.77l77lq   777qQp7q7...7 Vqql V......................qVUUq..7777777777l[[77..p7.VQ77l Q7777
..77l  q7.77l77qq   l777Vl.7q....qQ   V.............................77777777777q.77..7q7.  77l  q777
..77V  lq[77777qq   V.77lQ..q[.....[[...............................7777777777q7[[..qq777  m7l  l777
..77   Vq777777qq   Q7q77Q7.7[.[[[...............[...............[.[7p[7777777q...ql7777l  V7V  Q777
777l   Vq77777lVl   Q7q77Qp.[[[[[[.................................[q7[777777lq..VV777q7Q  Q7Q   777
q77V   Qq7777qlQV   V7q77Ql..[[[[[.................................7p[[[q7777l7qQ l777q7    q    q7l
l77Q    l7777qUQV   l7q77ll........................................q7[.[p7777lq   l77qql    q    q8l
Q77     l7777qQ Q   lqq.77Q[.......................................q[..[l7777VlQ  l77lqQ    V    l.V
 77     Q7777V      q7l.77 p......................................[7...[l777VQVQ  V77lq     Q    V.Q
 q7      q777Q      q7l.77UQ7.................[[.......q..........[[...7l77VQ QQ  R77QV          V7Q
 lq      l777       77q777pQQ7.................[.......................ql77    Q  Q7q            V7
 Vq       777       ql7777q7QQq.......................................qU77l        7l            Qq
  l       q77       lQ7.77q7l  V7...................................7V Q77         qV            Ql
  V       V.q      VV 7.77l7V    Uq...............................7l   V7l         qV             V
           7q      R  q[77QqV       Vq..........................qV     V7Q         VQ        
           qq         l777 qV          Vq[...................7lQ       Qq                    
           ll         Q777 VV             Vp[..............qV          VV                    
           QV          q7q VV                UVpq......[qVQQQ          V                     
                       V7l  V                    QVllVQQQQUQU          V                     
                        7l  V                 Q QQQQQQUllppql                                
                        lV                 VVlqlllUllppp667qpQ                               
                         Q                V.[q[.[77776ppppppqq7                              
                             VVV          V...q777[[[7qplqq[..q         Vqq[qV               
                           Vq....7VVV     V..qq7[77qq77qq[.qr.q      Vq.......q              
                          V..........q    V...vqq..q.qq.vv....q   q............q             
                          ..[.........q   V......v.vq.........qV V............[.V            
                         q.......7....V V.qv......vvq........qq.VQq...........[.q            
                        V.......7qqq77  q.........rrq.....qqqq..lQl...........[..            
                        ........q7777qlV..........qrq...vqqv....q V7qqq7......[..V           
                       l........p7777777lqqqqqq..q[qq.........qqqlqq77q7.........q           
                       .....[..[l77777777qq777qq.......7qqqqqllq7777777[.........[           
                      V.....q..7l7777777777777777.7[7qqq7777777777777777..........           
                      ......7[.7l7777777777777777lq7ql7777777777777777qq..........q          
                      ......[7.ql7777777777777777q77777777777777777777qp...........V         
                     q.......q[ql777777777qllllqq7777q7qqq777777777777lq....[.......         
                    p........q77l7777777llQQQQQQlmlllVQQQQQRlll7777777l7.[.[[........        
                   V.........[pql7777777lllllVQQRllllQQlllllllll77777ql777[q.........q       
                  V...........qVl7777777777mlllllllllllllllll37777777ll7q[7...........q      
                 V............7Ql777777777lll77mllllRm77lllm333777777lpp77[............V     
                V..............ll77777777ll777llllllll7777lll7777777ll777[..............           

Tuesday, November 20, 2012

Madoka Rainmeter

Hello there,I was wondering what would be my 1st post on this blog, and apparently today out of nowhere I was inspired to create a rainmeter, and so here it is:
Photobucket Rainmeter name: Madoka Rainmeter

File Size: 192 KB

Color Theme: Pink
I was actually planning to use an Image of Ultimate Madoka, but unfortunately I can't find any rendered image from Danbooru.
Well, I guess, this might seem noobish compared to http://randomnessthing.com/rainmeter/ since It's what inspired me, and I haven't known much stuff that could be done by rainmeter like playing music.
Though, it seems updating a rainmeter looks pretty simple(if there's an image ready to use), so yeah I might update this in near day.
tl;dr here's the download link: ^^
Download Here