sexta-feira, abril 22, 2011

DD-WRT - A Linux-based firmware for wireless routers

Are you satisfied with configuration options of your wireless router?



If the answer is "No", you should know that there are many third-party firmwares that you can install on your device, and give it options that you would find only in high-end routers.
In this post, I'm going to introduce DD-WRT. It's a free third-party linux-based firmware, under the terms of GPL license. It include features like daemon-based services, IPv6 support, Wireless Distribution System (WDS), RADIUS, Quality of Service (QoS), radio output power control, and many others.

I tried DD-WRT in my D-Link DIR-300 wireless router and I got many new functionalities, such as iptables, traffic graphs, better QoS support, blocking websites by URL or keyword, and others. One interesting thing is that now I can use my Wi-Fi router as a Client or as a Repeater (even the other wireless routers of the network doesn't support WDS). I present you some printscreens from its configuration pages:


Wireless Modes



Used CPU and Memory



Traffic Chart of this Month


DD-WRT can be downloaded at: http://www.dd-wrt.com. Note that firmware upgrades have a risk of damaging your device. Please read carefully all about it on the website. There you will find if your wireless router is compatible with DD-WRT or not.

String format for DateTime in C#

In C# language, there are the following custom format specifiers for date and time:
y - year
M - month
d - day
h - hour 12
H - hour 24
m - minute
s - second
f - second fraction
F - second fraction, trailing zeroes are trimmed
t - P.M or A.M
z - time zone


Some examples on how to use them (the output is commented at the end of each line):


/*
In this example, we create an DateTime object (whose name is "dt") that representes
the date and time: 2008-03-09 16:05:07.123
*/

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone

segunda-feira, abril 18, 2011

Some important Taylor/Maclaurin series

Have you ever asked yourself about how your scientific calculator or your computer "know" the sine or the cosine of a number?

Maybe you thought that those devices had predefined tables inside their memories to calculate those functions. But in fact those tables don't exist! So, how those devices are able to give you the correct answer with great precision?

The truth is that they use algorithms that calculate the results of Maclaurin functions. According to Wikipedia: "A Taylor series is a representation of a function as an infinite sum of terms calculated from the values of its derivatives at a single point. Taylor series were formally introduced by the English mathematician Brook Taylor in 1715. If the series is centered at zero, the series is also called a Maclaurin series, named after the Scottish mathematician Colin Maclaurin who made extensive use of this special case of Taylor series in the 18th century."

This is an important subject of Calculus, but the objective of this post is to show some of those functions, so we won't make demonstrations here. If you really want to study it further, I recommend you to check the book: "Calculus - Volume 2, by James Stewart". Bellow you can appreciate that interesting way to calculate some well known functions:

The exponential function:


Sine and Cosine (and hyperbolic ones):








We can tell you those are ways to approximate the functions values, and precision depends only of the value of n. Note that those sommations go from zero to the infinity, then the value of those sommations converge to the real values of those functions, but computers can't calculate them to infinity. Thus, we calculate each sommation to a finite (and relatively "small") value of n, in a manner that satisfies the desired precision that we need. One example is given bellow:


Here we calculate the exponential function for x = 2 (that is: e^2)


For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 3.0
For n = 3 , the sommation is: 5.0
For n = 4 , the sommation is: 6.33333333333
For n = 5 , the sommation is: 7.0
For n = 6 , the sommation is: 7.26666666667
For n = 7 , the sommation is: 7.35555555556
For n = 8 , the sommation is: 7.38095238095
For n = 9 , the sommation is: 7.3873015873
For n = 10 , the sommation is: 7.38871252205
For n = 11 , the sommation is: 7.38899470899
For n = 12 , the sommation is: 7.38904601571
For n = 13 , the sommation is: 7.38905456683
For n = 14 , the sommation is: 7.38905588239
For n = 15 , the sommation is: 7.38905607033
For n = 16 , the sommation is: 7.38905609538
For n = 17 , the sommation is: 7.38905609852
For n = 18 , the sommation is: 7.38905609888
For n = 19 , the sommation is: 7.38905609893
For n = 20 , the sommation is: 7.38905609893

According to the built-in function of Python's math library, e^2 is: 7.38905609893

In this example, we conclude that n equal or greater than 19 gives a good precision (if compared with Python's built-in function) for x = 2. But for greater values of x, we should increase the number of iterations. Let's check another example, for x = 5:


For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 6.0
For n = 3 , the sommation is: 18.5
For n = 4 , the sommation is: 39.3333333333
For n = 5 , the sommation is: 65.375
For n = 6 , the sommation is: 91.4166666667
For n = 7 , the sommation is: 113.118055556
For n = 8 , the sommation is: 128.619047619
For n = 9 , the sommation is: 138.307167659
For n = 10 , the sommation is: 143.68945657
For n = 11 , the sommation is: 146.380601025
For n = 12 , the sommation is: 147.603848505
For n = 13 , the sommation is: 148.113534955
For n = 14 , the sommation is: 148.309568205
For n = 15 , the sommation is: 148.37958008
For n = 16 , the sommation is: 148.402917371
For n = 17 , the sommation is: 148.410210275
For n = 18 , the sommation is: 148.412355247
For n = 19 , the sommation is: 148.412951072
For n = 20 , the sommation is: 148.413107868
For n = 21 , the sommation is: 148.413147067
For n = 22 , the sommation is: 148.4131564
For n = 23 , the sommation is: 148.413158522
For n = 24 , the sommation is: 148.413158983
For n = 25 , the sommation is: 148.413159079
For n = 26 , the sommation is: 148.413159098
For n = 27 , the sommation is: 148.413159102
For n = 28 , the sommation is: 148.413159102
For n = 29 , the sommation is: 148.413159103
For n = 30 , the sommation is: 148.413159103
According to the built-in function of Python's math library, e^5 is:148.413159103


Now note that n = 19 isn't enough to give the same precision as before. And the precision for n = 19 will get worse for greater values of x. Thus, the greater is x, the greater is the number of necessary iterations to get a good approximation of the real value of the function.