the seventh seal

Tuesday, November 10, 2009

Calculation of PI

http://www.reddit.com/r/programming/comments/9uxjo/dear_progg... on Twitpic

Today I was drawing answers to a quiz using boxes that stood for binary representations of the answers and realized that any regular polygon is composed of triangles. I thought back to a question I saw on reddit where someone had challeneged the readers to determine the radius of a circle without knowing what PI was.
I figured this could be a way to find the perimeter of any polygon using its "radius". Knowing that PI is the ratio of the circumference to the diameter , a polygon with the number of sides approaching infinity will have a ratio of perimeter to diameter approximately equal to PI.


I started with a square. Its composed of 4 triangles and I used the law of sines to find the perimeter. The angle of the center is 360/4 (where 4 is n for squares being 4 sided) then other 2 angles are (180-(360/4))/2 or half the remaining degrees left in the triangle. If the radius is 1 then the perimeter of the square in terms of the law of sines is (sin(90)*4)/sin(135). If we continue this for a hexagon we find that the perimeter of a hexagon with radius 1 is 6. (sin(60)*6)/sin(60)).



We can take the limit of this function as the number of sides approaches infinity with the radius of 1. We take the result and divide it by the diameter of the "circle" which would be 2.



Here is the resulting code.


dr=lambda{|x| x*(Math::PI/180)} #degrees to radians (cuz Math.sin won't take degrees)
p=lambda{|r,n| (r*Math.sin(dr[360.0/n])*n)/Math.sin(dr[(180-(360.0/n))/2.0])}
pitime=lambda{|r|
last=0
4.upto(1.0/0) {|x| #from the perimeter of a square to infinity
y=p[r,x]/(r*2) #calculate ratio of perimeter to diameter
break if y==last
last=y
}
last
}
puts pitime[1.0]
>> 3.14159265341633

(Took 172599 iterations in 1.5seconds to get that number)

Friday, April 17, 2009

How to implement interactive breakpoint - like functionality.

When calling a script from irb/command line. You can use this snippet to take statements from stdin and evaluate them in the current context just like IRB. Use qq to stop.



if options[:debug]
STDOUT << ">> "
while (cmd=gets.chomp)!="qq"
puts eval(cmd).inspect
STDOUT << ">> "
end
end

Thursday, March 05, 2009

Limits and Derivatives and Ruby

I read this article about Lisp: http://funcall.blogspot.com/2009/03/not-lisp-again.html
I wanted to try doing the same in Ruby.
I didn't like his setting DX as a constant close to 0 to shortcut not creating a function to figure the limit.




lim=lambda{|f,h|
y=h
1.upto(1.0/0) {|p| ##to infinity
x=h+(1.0/10)**p ##increase how close x is to h 1.0,0.1,0.001 etc..
fx=f[x] ##get f(x)
break if y==fx ##if we start getting the same val, stop
y=fx ##store the last val
}
y ##return where we stopped
}
ddx=lambda {|f|
lambda {|x|
lim[lambda{|dx| ##the limit of the derivative function
(f[x+dx]-f[x])/dx
},0] #as h approaches 0
}
}


Here are some results
(Infinite limits) http://www.cliffsnotes.com/WileyCDA/CliffsReviewTopic/Infinite-Limits.topicArticleId-39909,articleId-39873.html

irb(main):319:0> g=lambda{|x| 1/(x**2)}
=> #
irb(main):320:0> lim[g,0]
=> Infinity
irb(main):321:0> g=lambda{|x| (1/(x**2))-(1/(x**3))}
=> #
irb(main):322:0> lim[g,0]
=> -Infinity

Derivatives

irb(main):326:0> g=lambda{|x| x**3}
=> #
irb(main):327:0> ddx[g][2]
=> 12.0000009928844


I tried for hours to get the limit to resolve to a nice integer... but could never quite get it to be more precise. I did this because i googled "derivatives calculus ruby" and couldn't find anything about this. Why not?

Sunday, December 21, 2008

Upgrading Server to Ubuntu Hardy Heron LTS from Dapper Drake LTS

My company hosts several rails applications. For the ones in high demand - we use mongrel_cluster with nginx. The only problem is ... we use apache for everything else. So we proxy pass requests into nginx from apache. That seemed so redundant that I decided to get rid of nginx and use mod_proxy_balancer instead.



On 6.06 this turned out to be much harder than it seemed. Essentially proxy_balancer.so did not exist in /usr/lib/apache2/modules .. I would have to compile it with apxs to get it into the installation. I found out that apache 2.2 came with proxy_balancer but when I tried to update the apache package ubuntu said it was already the newest version. I knew this meant I may have to consider an upgrade to the next LTS. Beyond using mod_proxy_balancer I had been trying to get "Phusion Passenger" to work for over a month. (I had to become very familiar with httpd.h and mod_passenger.c to get it to even compile). As of that point I still had no way of serving up rails applications from apache without using Proxy Pass.



It was late on Saturday night and I had the whole weekend to fix anything that broke so I felt pretty confident that everything should be fine.



I did the commands.




#sudo su
#aptitude update
#aptitude upgrade
#aptitude dist-upgrade
#aptitude install update-manager-core
do-release-upgrade




The upgrade was to be 287mb and take several hours. I pressed the "y" key and started browsing reddit on my laptop.
Through the installation I was asked what to do about configuration file conflicts between packages and my own custom versions. There were many times where I honestly didn't care because I didn't even know certain things were still installed. ldap.conf? hylafax.conf? I mean I played around with them .. thought I uninstalled those things. There were several obvious cases where I just kept my existing configs (my.cnf, apache2.cnf, php.ini etc)



The upgrade completed with an error message about /etc/fstab.pre-uuid already existing. I disregarded the error after googling the message for 10 minutes and finding nothing. Everything seemed fine.



I was delighted to finally get phusion passenger working and mod_balancer active. I took the liberty of installing about 10-15 packages I had experimented with but had no further use for. hylafax, bugzilla, otrs, auth-ldap-client etc... then I went home



The fallout

Later that night I went to show off some of the performance benchmarks to a friend and caught a page hanging. I pulled up my ssh terminal and tried to get in to see what was going on. I Couldn't get in! ! .



The next day I went on site to get on the server directly and see if I could get in. I entered every login and password I knew and it wouldn't even accept my username!. I followed instructions for manually resetting the passwords by going into recovery mode. I restarted the machine... none of the logins were checking out. I restarted again and looked at auth.log




Dec 21 06:36:55 www nscd: nss_ldap: reconnecting to LDAP server (sleeping 1 seconds)...
Dec 21 06:36:56 www nscd: nss_ldap: could not connect to any LDAP server as (null) - Can't contact LDAP server
Dec 21 06:36:56 www nscd: nss_ldap: failed to bind to LDAP server ldap://127.0.0.1: Can't contact LDAP server
Dec 21 06:36:56 www nscd: nss_ldap: could not search LDAP server - Server is unavailable
Dec 21 06:37:01 www CRON[9390]: PAM unable to dlopen(/lib/security/pam_ldap.so)
Dec 21 06:37:01 www CRON[9390]: PAM [error: /lib/security/pam_ldap.so: cannot open shared object file: No such file or directory]
Dec 21 06:37:01 www CRON[9390]: PAM adding faulty module: /lib/security/pam_ldap.so
Dec 21 06:37:01 www CRON[9390]: pam_unix(cron:session): session opened for user root by (uid=0)
Dec 21 06:37:01 www CRON[9392]: PAM unable to dlopen(/lib/security/pam_ldap.so)
Dec 21 06:37:01 www CRON[9392]: PAM [error: /lib/security/pam_ldap.so: cannot open shared object file: No such file or directory]


It hit me like a ton of bricks. At one point we had another IT guy here who wanted to use ActiveDirectory to manage the users. I hated windows and microsoft for a variety of reasons and wanted to prove to him that I could provide a much easier to use system using linux and phpldapadmin. I installed LDAP ... integrated it into the system and got it running - and we never used it. Now I've removed auth-ldap-client and the authentication client depends on ldap to check if the user is in ldap.



I looked at /etc/pam.d/ and /etc/nsswitch.conf .. where I found references to ldap in /etc .. I also found them in /etc/auth-client-config .. I read up on auth-client-config and found out that it can be used to control nsswitch and pam.d/* config files with profiles. I couldn't find a pre-ldap example so i modified the kerberos example and executed auth-client-config -a -p kerberos_example from the recovery prompt. And everything worked fine after that.



So please.. If you hear about a package, a project or the next biggest thing and you must install something on your machine. Consider doing it in a sandbox VM

Monday, December 15, 2008

Use "less" instead of more

Instead of doing "cat somefile | more" try using "less somefile". Less is a spinoff of more which supports vim-style find (press "/" and type what you need to find) and can read sections of file from disk as opposed to reading the whole file in then displaying it. It's also less typing.

Saturday, December 13, 2008

Microsoft

Selling software isn't dead. Its just almost dead for many people. People saw that many great things were possible with computers. You could streamline small businesses, help groups collaborate, coordinate research and so on. I feel the world doesn't think its fair that Microsoft be the final word in software. People weren't comfortable having their aspirations run under one company's flag.
Software isn't dead, in the next few years you will see some of microsoft's largest markets turn their back on the giant.
Microsoft will be big in gaming. The Xbox 360 is amazing, well done.
Microsoft will be big in the business world.
The Microsoft "Personal Computer" will be a relic, a symbol of a dark time for all of human kind and the apogee of Microsoft's reign.
Home PCs will run on Apple operating systems (based on linux) or Linux operating systems.

Tuesday, October 28, 2008

New Google Gadgets in GMail

Ran across some new features in Google Apps/Google Mail today. There are now mini-managers for docs/calendar that can be embedded into your G-Mail.


Free Image Hosting at www.ImageShack.us

I'm using the following plugins here.


  • Right-side labels

  • Right-side chat

  • Navbar drag and drop

  • Google Calendar gadget

  • Google Docs gadget