Wednesday, October 28, 2015

Finally, you can run Einstein Equations with CalcRx

This post assumes that you have the basic idea of C#, LINQ, and Reactive Extensions; and you have read the previous article in this series. You can suggest edits to this post here.
Click here to find out more about CalcRx.

Abstract

This is the second and the last post with regards to Einstein Equation Fix. In the last post, we identified a major issue on how LINQ syntax was incorrectly generated by CalcRx and how we fixed that. But there was still one glitch that was causing the Einstein equations to fail. A fix is finally applied and now we can run Lorentz Transformations or similar equations. This post discusses the fix.

The issue

As you might recall from the previous post, we were still not able to run any of Einstein's relativity equations because of the way the bug was still there when functions were involved. To understand the issue better, consider the following expression.
1/sqrt(4)
I have left out the 'self' reference or the underscore for the sake of simplicity. The above will generate an Expression like this
__main.Select(a0 => fnsqrt(4)).CombineLatest(__main, (a,b) => 1/a);
Which is basically saying, transform the observable to yield square root of 4, then combine it with target observable again and do the division.
The above example would look pretty if CalcRx generated the following expression
__main.Select(a0 => 1/fnsqrt(4));
The last fix was not taking functions into account. Since in computer programming world square roots are done through functions, most of Einstein's Relativity equations that involve square roots would fail.
For instance, this time dilation equation
t=to1(v/c)2)

The fix

The fix this time was easier, or at least we knew what it was. The fix was same but we just have to apply that to functions.
So now finally you can write Einstein Equations in expressions like so
// Length Contraction
var transformedLength = CalcRx.Evaluate<Measurement, double>("Length/(1 - sqrt(Velocity/299792458)^2)", functions);

Conclusion

No math expression library would do justice if it could not function on any kind of expressions. Specially the Einstein's equations. Good thing the issue is resolved.

Friday, October 2, 2015

Here are a few caveats to using Aria Templates in your web application

Abstract

This post discusses my experience as a first-timer with Aria Template,  what type of challenges I faced, and the lessons I learned that the reader could benefit from.


You may have already known about the robust and awesome way of having templates using Aria Template. If not, check out this short guide here. It indeed looks fancy, sounds like it can help you a great deal but it comes with some warnings.

MIME Types

Most web servers nowadays don't just serve all types of files when requested through a URL. Only the ones that they are configured to serve. Aria template files are saved with .tpl extension, and it also loads one of its internal file with .cml extension in the bootstrap code so you will have to configure your web server software (IIS, apache, nginx etc) to allow those extensions as text/html.


Template Path confusion

Aria turns what you provide in classpath into a relative path to your .tpl file. In my experience, I had thought the following classpath would represent /home/userarea/Profilesnapshot.tpl

{{Template classpath:'home.userarea.Profilesnapshot' }}

But it was actually looking for the said tpl file in /js/home/userarea/Profilesnapshot.tpl where 'js' is same directory where area templates JS file was included from.

Also, the last part of the classpath (in this case Profilesnapshot) should have the first letter upper case (same goes for the template file name).


Conclusion

Be prepared to spend at least 5 hours scratching your head, and trying to figure out why something is not working. Also, if your team has many red-tapes or is one of the ones that are "too busy to improve", it is not easy to sell Aria to them. Not to mention the grouchy admins who will give you all sorts of trouble when you ask them to add new MIME types to the web server.

Sunday, September 6, 2015

CalcRx: The Einstein Equation Fix

Abstract

This is about a bug in Project CalcRx which was causing it to compute the wrong results for Einstein equations or expressions with similar properties. The resolution, and future improvement possibility are also discussed. You can suggest edits to this article at this page.

The issue

There is nothing peculiar about Einstein equations in this scenario that causes this, it is just how CalcRx was coded was throwing it off. Anything with Base expression (or your parameter variable) involved twice across one or more binary operations would fit the scenario. But in my case, I was writing tests for Einstein Equations and failed it at the beginning. To understand the issue better, consider this scenario. You have an application that is monitoring the velocities of an object moving relative to your probe, you want to apply Lorentz Transformation. The following equation is not true Lorentz Factor because of the missing square root in the denominator, we will see later why.
_/(1 - (_ / 299792458) ^ 2)
That huge number is speed of light in a vacuum in meters per seconds. Before the issue was fixed, it was generating an Expression like this
__main.CombineLatest(__main, __main.Select(a0 => a0/299792458).Select(a1 => Math.Pow(a1, 2)))
Pretty ugly right? not only above expression looks ugly it will produce undefined results because of the way simultaneous operations are timed. To understand this better, we need to look into CombineLatest. CombineLatest does not care how the data is coming in, as soon as one of the observables in the combination yields an output, CombineLatest recomputes the expression. So if an expression has to wait for all variables involved to be updated before it could be recomputed will fail. The other option is to use Zip, but CombineLatest is thrown in there for a purpose, there are cases where you need not to wait for all sides to post an update to recompute the formula. So what is the option? The answer is to wrap everything cohesive around in one Select.
The above example would look pretty if CalcRx generated the following expression
__main.Select(a0 => a0 / (1 - Math.Pow(a0 / 299792458)));
But the way expression tree was being built was, every time an IObservable<T> was combined in a binary operation with a number, it would do Select which turns the expression into IObservable, then all subsequent binary operations will do Select on Select. And if you try to combine two Select-s that would try to join them with CombineLatest.

The fix

To fix the issue, I implemented the following.
  1. Everytime you run into a binary computation between a Select-or and a number, re-write that Select-or to include that select inside.
  2. Everytime two observables that are results of Select-s are combined in a binary operation, discard one Select and combine the selectors of both in one Select.

New issues

The fix helped me generate a very clean and acceptable Expression. I have ended my fix there, but there are still two issues left.
  1. The fix is a burden on Parsing because it regenerates the whole Select by prying the existing one open, excavating the selectors out of it and combining it with the other operand into another Select.
  2. Einstein Equation will still fail
To understand 1, consider the scenario where parsing is underway for the following expression
_ + 3 * _
Now the first step would be to generate the Expression for 3 * _ which would generate
__main.Select(a0 => 3 * a0);
Perfect so far right? Now the system has to combine a _ +, the new fix would rip the Expression, remove 3 * a0 which is the selector out, then generate new expression and a new Select call Expression. Imagine how crazy would it be if your expression involve 20 binary operations? The troubles don't end there consider the following expression
(_ + 1) * (_ - 1)
In the above case, you will end up with having to combine two __main.Select Expressions with binary multiply operation. Before the fix, it would combine them using CombineLatest, but thanks to the fix, now it will convert both of them into one Select. But to do that, it would excavate the selectors of both, get the Parameter expression of one, and recursively walk the other selector tree to replace its own parameter expression with the one of the other side. To understand it better you are having to combine the following two
__main.Select(a0 => a0 + 1) * __main.Select(a1 => a1 - 1)
Notice that the first selector is referencing a0, and second is a1. In the new Select, there would be only one parameter, either a0 or a1. In order to do that, I have done something I am not too happy about. I am walking the whole tree on the other side, regenerating every bit of it because Expression trees are non-mutable and replacing a1 with a0.
Now about 2, The Einstein Equation failure. Consider the easier equation that would fail this scenario. How about we divide the number by its square root?
_ / sqrt ( _ )
Now in order to accomplish the above, the square root function or sqrt has to come from either the Function Packs or has to be included in the call to Evaluate, when underscore (_) is an argument to a function, the current architecture expects to find a function that takes observable for that argument and will pass __main to that parameter. A sane implementation however should find a proper overload and do something like following
__main.Select(a0 => a0 / sqrt( a0 ))
That is indeed not expecting too much. For the above I have opened the issue#3 and the fix to that will finish the Einstein Equation fix.

Conclusion

So in a nutshell, the issue was the crazy Expressions being generated, I implemented a fix which although one-time but does add an overhead on the parsing and Expression generation part and Einstein Equation or similar would still fail because of the way function calling is implemented.

Tuesday, August 25, 2015

Allowing end-users to perform custom computations on real-time data

Absract

This post is an introduction to a new project CalcRx that allows you to write applications to give users ability to run custom expressions on Observables. A couple of example scenarios are also discussed. To learn technical aspects about CalcRx in detail, visit the project page. This post does not assume you know about .NET and Reactive Extensions already. But in order to understand the technical aspects of CalcRx, those two are required.

You may have learned the about importance of usability, abstraction, and inference of real-time data already. In this post, I am going to jump straight in to one of the ways that we can accomplish this in .NET Applications.

Scenario 1

First consider this scenario, you have an application that displays the real-time chart of radiation levels monitored in Earth's atmosphere. The satellite is pounding your application with tons of data. The user of your application wants to see only so much, suppose your user wants to see the data where radiation levels are higher than 2 microsieverts, but your application is getting the 100 readings per seconds and your chart is animating too fast to be effectively read by the users. Now the users are telling you to show only the levels higher than 2 microsieverts, or one user wants to see only those spots where the radiation level is 2+ for at least 1 minute other wise he or she wants to ignore the reading.

Scenario 2

Another scenario is, you are the developer of the application that is showing the live chart of stock prices. Every time there is a tick, the chart has a point added. Suppose your user is monitoring Apple's stock which is changing every 2 seconds, your user does not like to be overwhelmed with a lot of unrelated information, he asks you to change your application to show you the Moving Average of last 5 ticks only. While your code change might work for the user watching Apple's stock, it will not do much good for the person who is watching a stock that does not change frequently.

CalcRx can help you in both of the scenarios described above. As long as your application is a .NET Application and the radiation levels or stock prices you are getting are received in IObservable<T>, CalcRx can make the job so much easier.

IObservable<T> is an interface for an implementation of a Publish-Subscribe model. You can convert anything coming into your .NET Application into IObservable<T>. For instance, if you have an event that is fired up every time you receive a new reading of radiation level, you could easily create an observable out of it. For more information, Checkout www.introtorx.com.

Once you are receiving your data through an Observable. You don't have to change your code anymore. You just add a Textbox to your application and ask your users to write their own custom formulas into that. CalcRx uses the expression language which is very close to what Excel uses for formulas.

For a detailed into on CalcRx, visit the Project Page

Tuesday, June 16, 2015

Lets be honest, Java is a piece of crap. Here is why

Many people including the fanbois or the ones who have put all their eggs in the Java basket and think that there is no way out would get angry at my post, and will get very very defensive and I understand that. But have a little open mind and think about it for a moment, isn't it time we kill Java? Here is why



It was cool

Sure it was the next big thing when it first rolled out, it came up with some new ideas (and overly exaggerated claims). The first and foremost was the Platform independence. Which made many feel warm and fuzzy, it also did a great job in promoting the rapid development. It put many people who would otherwise not code into coding.


It was crazy

Lets first talk about this crazy idea of Platform Independence. If there is one plain simple way to describe it, it is described in this bash.org quote (CAUTION: Its not PG). But really the point is about the usefulness of this feature. Sure, it will work for hobbyist coders who are writing cool command line utilities and they just want to give out binaries. But in today's programming world of writing portable code and compiling from the source (which has been that way in *nix environments for a very long time); this idea does not fly anymore. I mean, how many enterprise applications do you see who target their servlets to run on Linux and Windows machines at the same time? They choose one or the other and they stick with it.

Mistakes and fallacies in Java and Sun's failure to address those caused Microsoft to create C# (ok may be not "create" but you get my point). And C# caught on very good, many fanbois argue that C# copied the semantics of Java, I would say, Rub it in!! because Java copied the semantics of C++. Just moving those little access modifiers to the function signatures doesn't really make you creative. C# not only addressed the issues the rapid-development world was having, it added many great features afterwards to make people's lives easier and nowadays its Java trying to catch up by "copying" C# features.


Fanbois are dying for attention

Sure Java is dying slowly, Sun's acquisition by Oracle is a sign. Oracle possibly bought Sun because Java has been Oracle's lovechild for quite a long time and they had quite a few of their eggs in Java's basket. So what are the fanbois doing? They are dying for attention, while the developers of languages like Python, Haskell, and C# etc are busy making this world a better place, the hustlers of Javaland are busy using allegedly "cool" ways to create some PR. Here is one example. And of course the "3 Billion Devices Run Java" banner whenever you install Java. I say this to that: 3 Billion devices Run Java, rest of them work fine.


The Update-Diarrhea

And lastly, the everyday Java updates that are annoying as hell. You go to one website where their crap is written in Java and you are required to install Java to see their content. You make that mistake and now you are facing the Update-Diarrhea of Java. You would never possibly be going to any website again in six months that require Java to run, but Java will keep Java Update Scheduler (jusched.exe) running in your PC all the time taking up CPU time, memory, and sucking your internet. And the real fun begins when a new update is out, which is out like every freaking day, and the updater will bug you over and over. I have to be honest, I have a pet peeve with this Update-Diarrhea thing; what the hell is so important about you that you are spitting out updates too often and you are showing those pesky popups begging the users to click on that 'Install' button? I tell you why, Java is struggling, it is dying to get your attention.

Oh no they're here, hide Java immediately!

Lastly, I would finish my rant with this. Whenever we have aliens visiting us, we will be under pressure to show them the human ingenuity. We will show them the Great Wall of China, we will show them the mixed-fraction of Pi (3.14...), there is quite a lot to show off to them to impress them. BUT there has to be a list of things that we should hide so we don't sound unenlightened halfwit idiots and not worth exploring, and Java tops that list.

Feel free to disagree with me and let me know your thoughts in the comments below

Sunday, May 10, 2015

SignalR-based Live Stock Ticker with Ticker.FSharp

Introduction

It was an awesome weekend, finally got my hands into SignalR and updated the Ticker.FSharp to add a SignalR web sample ticker. In this post I have talked about how easy it was to throw a simple ticker together using the Ticker.FSharp repository and SignalR. The sample can be downloaded from the Github repository page.

Screenshot





Implementation

I added an empty web project to the solution, then added a SignalR Hub, a Javascript file for event handling, an HTML page for the UI. Here is a quick overview of each of those.

SignalR Hub

As we know, the server-side hub is a primitive component of a SignalR application. My sample hub has only two methods, one is called by the client to start up the hub when it gets launched the first time, and other method is called from the server side that reports the ticks generated by Ticker.FSharp. The call to Tick then channels in to the browsers of each user connected.

The full source listing for the hub file can be found at this Github page. I am only discussing the important points.

Here is how the Hub is declared

[HubName("tickerFsharp")] public class TickerFSharpHub : Hub
As you can see, the hub declaration contains the HubNameAttribute which tells the SignalR engine how the hub is going to be located. There are two methods inside the hub, one is StartTicker which is responsible for calling the Ticker.FSharp library and obtaining the IObservable<Tick> object. Here I am fetching the ticks of three symbols together.

[HubMethodName("startTicker")]
public void StartTicker()
{
if (subscription == null)
{
var machine = new DataMachine();
var aapl = this.AddIndicator(machine.GetLiveGoogleApiTicks("AAPL"));
var goog = this.AddIndicator(machine.GetLiveGoogleApiTicks("GOOG"));
var msft = this.AddIndicator(machine.GetLiveGoogleApiTicks("MSFT"));

subscription = aapl.Merge(msft).Merge(goog).Subscribe(t => {

this.Tick(t.Tick.Symbol, t.Tick.Price, t.Tick.LastUpdated.ToString(), t.Up);

});.
}

In the above listing, I am calling the Ticker.FSharp 3 times to get the live feed for three symbols. I am also calling the AddIndicator helper method to garnish the object with an additional property that tells me whether the value went up or down since the last value. And finally, I am calling the Tick method which calls the tick method at the client's side.

public void Tick(string symbol, double price, string lastUpdated, bool up)
{
Clients.All.tick(symbol, price.ToString("###0.00"), lastUpdated, up);
}

The above Tick() method implementation is pretty self-explanatory.

Javascript Event Handling

The JS file has quite the code, I am only discussing the tick() handler that server-side will be calling.

hub.client.tick = function (symbol, price, lastUpdated, up) {
var row = $stockTableBody.find("tr[datasymbol=\"" + symbol + "\"]");
if (row.length == 0) {
var templatedRow = rowTemplate.replace("{Symbol}", symbol)
.replace("{Price}", price)
.replace("{lastUpdated}", lastUpdated);
$stockTableBody.append(templatedRow);
}
else {
var cells = row.first().find("td");
cells.eq(0).html(symbol);
cells.eq(1).html(price);
cells.eq(2).html(lastUpdated);
}
}

The above code is simple, as the tick arrives, it checks to see whether the table already has a row for that symbol, if so it updates the data otherwise it adds a new row for that symbol.


Thats all for this blog, if you want to check out the full source code, I encourage you to visit the Github project page.

Happy coding!