Drawing Blanks

Premature Optimization is a Prerequisite for Success

Archive for the ‘Uncategorized’ Category

Optimal angle for throwing things from a height

leave a comment »

We know that we need neither calculus nor trigonometry in order to prove that the 45 degree angle maximizes the distance.

https://bbzippo.wordpress.com/2010/01/14/45-degrees-proof/

https://bbzippo.wordpress.com/2010/01/19/45-degrees-optimum-geometric-proof/

That is, when we are on a flat surface, or more precisely, when the landing point is at the same height as the launch point.

Finding the angle that maximizes the horizontal distance when the launch point is elevated is generally considered a much harder problem. See e.g. here for a solution http://luckytoilet.wordpress.com/2010/08/07/throwing-a-rock-off-a-cliff-calculating-the-optimal-angle/

I’ve just learned that there exists a trick that allows to solve this problem without differentiation.

First let’s consider the family of trajectories of the body launched at different angles at the same initial speed:

p

The red-highlighted curve is the 45-degrees parabola.

And what is the green curve? It is the envelope of our family of trajectories.

It is called Torricelli’s Parabola of safety http://en.wikipedia.org/wiki/Parabola_of_safety

Judging by its name, it is a parabola, and Torricelli likely proved that fact without any calculus. I’ll demonstrate that in one of the next posts, and now I’m going to quickly show how to use it to solve the original problem.

Using the fact the the 45-degree parabola is optimal, we trivially find that the point (V^2/g, 0) belongs to the green parabola. And (even more trivially) since the point (0, V^2/2g) belongs to it too, we can write its equation y = -\frac{g}{2V^2}x^2 + \frac{V^2}{2g} , substitute our initial height h and find the max distance S = V/g \sqrt{V^2 + 2gh} . Finding the angle corresponding to this distance is just a boring exercise that I leave to you.

After the break – some ugly R code that I used to plot the picture.

Read the rest of this entry »

Advertisements

Written by bbzippo

10/09/2013 at 5:07 am

Posted in Uncategorized

SQL Server collations and string comparison issues

leave a comment »

Evil Kitty 😼 equals to the empty string

With the “default” collation setting (SQL_Latin1_General_CP1_CI_AS) SQL Server cannot properly compare Unicode strings that contain so called Supplementary Characters (4-byte characters). For example, the “kitty face” below is equal to the empty string:

(If you don’t see the kitty faces then your browser and/or OS sucks as does Chrome on Windows. The symbol is U+1F63C “cat face with wry smile” http://en.wikipedia.org/wiki/Emoticons_(Unicode_block) )

SELECT ‘oops’ WHERE N’😼’ = N”

Moreover, the default collation considers phonetically equivalent Katakana and Hiragana characters to be equal:

SELECT ‘oops’ WHERE N’お’ = N’オ’

These issues are easy to work around by using a binary collation. But in most scenarios we want case-insensitive comparison, so binary is not an option.

Here is a collation that is case insensitive and doesn’t have issues with Supplementary Characters or with Kana:

Latin1_General_100_CI_AS_KS_WS (CI – case-sensitive, AS – accent sensitive, KS – Kana-sensitive, WS – width-sensitive).

In SQL 2012 this one also works: Latin1_General_100_CI_AS_KS_SC (SC –  supplementary characters).

SELECT ‘oops’ WHERE N’A’ COLLATE Latin1_General_100_CI_AS_KS_WS != N’a’ — Case insensitive

SELECT ‘oops’ WHERE N’お’ COLLATE Latin1_General_100_CI_AS_KS_WS = N’オ’ — Kana sensitive

SELECT ‘oops’ WHERE N’😼’ COLLATE Latin1_General_100_CI_AS_KS_WS = N” — Supplements differ from ‘’

SELECT ‘oops’ WHERE N’😼’ COLLATE Latin1_General_100_CI_AS_KS_WS = N’😽’ — Different Supplements differ

SELECT ‘oops’ WHERE N’😼’ COLLATE Latin1_General_100_CI_AS_KS_WS != N’😼’– Equal supplements are equal

One remaining issue that I’m aware of is the German Eszett: ß equals to “ss” under any non-binary collation.

SELECT ‘oops’ WHERE N’ß’ COLLATE Latin1_General_100_CI_AS_KS_WS = N’ss’

Microsoft acknowledged the Eszett issue as a bug in 2008: http://connect.microsoft.com/SQLServer/feedback/details/341130/-and-ss-are-not-equal

But the real question is:

Is it the correct design to use the same collations for the purpose of sorting and for the purpose of equality comparison?

Written by bbzippo

09/10/2013 at 1:22 am

Posted in Uncategorized

Red vs. Green

leave a comment »

image

This is from Microsoft Excel 2010.

And I have a confession to make: I can hardly tell Good from Bad… like 1% – 6% of all human males I have a slight defect of color vision. We are not a threat to society: we can tell apart the traffic light signals and other color-based discriminators used in machinery, weaponry etc.

So why don’t UI designers take us into consideration? I’m sure there is a ton of research on creating dichromacy-friendly color palettes.

The RGB values for Good are 198, 239, 206. For Bad: 255, 199, 206. Are you kidding? How can anyone tell these apart? Now let’s increase saturation (+40 in Photoshop):

image

Now I can clearly see the difference. Good RGB: 200, 255, 130. Bad RGB: 255, 199, 206. See, Bad wasn’t affected by the saturation boost. And Good became… greener and lighter.

… Lately I’m sooo disappointed by what Microsoft is doing in UI/UX. It’s cargo cult… Failed attempts to copy Apple… I’m so glad Steve Sinofsky is no longer there… I wanted to write a huge rant about Windows 8, but I don’t even know where to start.

Happy Holidays!

Written by bbzippo

12/26/2012 at 2:35 am

Posted in Uncategorized

The measure of massiveness

leave a comment »

This year’s Nobel Prize has not been awarded for the discovery of the Higgs. It rather celebrates achievements that will undoubtedly have practical applications. In order to compensate for this unfairness I wanted to dedicate a couple of hours of my time recollecting and reminiscing of some fundamental things that are related to the notion of mass. As I’m not formally trained in physics, this is based on what I learned in school and read in popular literature.

Mass in Intuitive mechanics

  • Mass measures the amount of stuff
  • Mass is conserved
  • Mass is additive
  • Mass is a measure of inertia

Mass in Newtonian mechanics

  • All of the above
  • Mass is invariant in all inertial frames of reference
  • Mass is the source of gravity
  • F=ma

Note that F=ma is nothing more than:

  • Definition of force F = dp/dt
  • Definition of acceleration a = dv/dt
  • Definition of momentum p = mv
  • Conservation of momentum (with no F there is no change of p)

Note that it is easy to guess that gravity is no different from inertia. Acceleration does not depend on the mass? Come on, it’s not a real force! It’s the same kind of force that gives the same acceleration to skinny and fat people on a bus that suddenly breaks.
Also note that it’s easy to predict that massless thing are affected by gravity: once you consider F=ma and F~mM, you see that a doesn’t depend on m. Which Laplace did. Laplace was brilliant, as I mentioned here and here, but this simplistic prediction did not match the observation quantitatively (by the factor of 2).

Relativity

  • We need to redefine momentum and energy so that they are still conserved
  • Mass is still invariant.
  • The rest energy is not zero. The mass is the rest energy.
  • The definitions of force and acceleration are still the same, but F is not ma.
  • Mass is not a measure of inertia. In fact, there is no invariant measure of inertia. Bodies resist forces depending on how they are moving.
  • Mass does not define the gravitational force. In the same manner as inertia, gravity depends on how the bodies are moving – it’s defined by the energy and momentum.
  • Mass is not additive. If we have a system of moving or interacting bodies, its mass is not the sum of the masses of the constituents.
  • Massless things cannot be at rest, in any frame of reference.
  • Mass is not the amount of stuff. It is the amount of motion and interaction – just like energy. But motion is relative… Once again, the mass is the rest energy.

The Higgs

  • When it seems that there are no interacting parts, but the massiveness is still there – it is due to interaction with the Higgs field.

Written by bbzippo

10/10/2012 at 3:08 am

Posted in Uncategorized

Microsoft promoting anti-patterns

leave a comment »

Found this today in production code that I needed to modify:

… = ((TextBox)(row.Cells[1].Controls[0])).Text;
… = ((TextBox)(row.Cells[2].Controls[0])).Text;
… = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

This was in a GridView.RowUpdating handler, and the change that I needed to make was adding a couple of columns to that grid.

Of course, I refactored the code in that page, and, of course, I found that all pages in the project are copy-paste-contaminated by that terrible pattern.

I did some lurking on google for grid view “best practices” and found something that is likely the origin of the infection:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx

Written by bbzippo

06/08/2012 at 4:53 am

Posted in Uncategorized

Xworder Mobile is out

leave a comment »

If you want convenient access to Xworder’s features from your smartphone, check out Xworder Mobile.

Written by bbzippo

05/30/2012 at 1:54 am

Posted in Uncategorized

Russian elections: 2011 vs 2012

leave a comment »

I managed to merge the 2011 and 2012 election data and I can now look at all changes that happened at precinct level.

As we saw here https://bbzippo.wordpress.com/2012/03/27/russian-presidential-election-2012, turnout increased, and the leader support increased even more.

What’s the relationship between the change in turnout and the change in leader support? Following Steven Coleman, we can use a nice additive measure – entropy – to compare those.

Below, on the horizontal axis is the change in turnout entropy. On the vertical axis is the change in choice entropy normalized to the number of candidates. The straight line is a regression with zero intercept (which is Coleman’s theoretical prediction, if I’m interpreting his theory correctly). The other line is the actual relationship (mean choice entropy vs. turnout entropy in 50 bins):

h-h-change 

Even though overall the change in choice follows the change in turnout as predicted, there is a very significant drop in the entropy of choice in the central region of the graph where the majority of precincts are located.

That indicates that in 2012 (presidential) elections the opposition was much weaker that in 2011 (parliamentary).

Let’s see in more detail, where the additional votes for the leader came from.

Below, on the horizontal axis is the change in turnout percent, and on the vertical – the change in the votes for the leader as a share of all registered voters. Note that this measure is different from the votes as a share of all cast ballots. I use the former because unlike the latter it behaves linearly when we add ballots for one candidate. The color indicates the level of support for the leader in 2011.

all-change

The most dense cluster here lies in the north-north-east sector of the graph. Those are precincts that showed low support for the leader in 2011, but in 2012 increased turnout and leader support. And the majority of points being located above the diagonal, means that the increased leader support cannot be fully explained by the increase in turnout – some people who had voted for the opposition in 2011 must have voted for the leader in 2012.

The opposite cluster shows that the situation with the precincts who strongly supported the leader in 2011 is fully symmetric: turnout dropped and the leader support dropped even more. This may be interpreted as a decrease in fraud.

And the right side of the graph being almost empty below the diagonal, means that the opposition did not attract many new voters.

Below are pictures broken down by the 3 categories of precincts: ethic outskirts, smaller precincts (presumably countryside) and larger precincts (presumably cities). They have no qualitative differences:

Read the rest of this entry »

Written by bbzippo

04/22/2012 at 11:26 pm

Posted in Uncategorized

Tagged with