Finding an Automated Algorithm to Solve Hess’ Law II

Update: You can see my current progress here. As of now, I have the modified Gaussian elimination completed, meaning part I is now done (unless more matrices break under it). It’s beta code and shouldn’t be considered complete until a battery of linear equations are run against it to check for validity.

Well, it’s been almost one week since I started working on this problem. To start off, I had several great answers from my question posted on LinkedIn, but it was shut down since someone flagged it as “commercial services”, which it really isn’t, but the person who shut down the problem clarified why it was, and that made sense.

Overall, I put out $100 because I wanted to put my money where my mouth was – it gave this problem value to others, and it also motivated me to work on it, as I was putting my own money on the line.

Anyways, I’ll be splitting the $100 to two people who were really helpful with providing their own pseudo-algorithms for this, Viacheslav Usov and Mkrtich Laziev, which helped me find a path towards developing an algorithm to do what I want.

I’ve spent probably over 15 hours on this program and managed to finish 99% of my initial code to solve the matrices that may represent Hess Law equations (I’ll explain why I said may at the end). I’ll never know if it’s possible to solve these problems until I run a lot of problems against the algorithm to see if I can get the expected values.

Then, there’s another issue once I feel I’m done with my algorithm. Although at first I thought I figured out how to translate a set of chemical equations to a matrix, there are some special cases where I cannot fit the equations into the rules that I use to form the matrix. Instead, I have to re-arrange it a bit to have a proper matrix that is solvable.

By the end of this, I hope to:

1. Have a PHP version of my own algorithm for Gaussian elimination with partial partitioning, backsubbing, as well as a least squares algorithm implemented. (99% done),

2. Be able to demonstrate that with any given Hess Law set of equations, that it can be applied to a matrix and that there will be one unique solution (assuming the set of equations has a solution, as textbooks tend to).

Needless to say, it’s been really fun doing this so far. I’ve never been so involved in working with algorithms or math for that matter. I had to do hours of research on linear algebera and figure out with my limited knowledge how to translate scary math notation into code.

Do you use analytic functions?

I’m currently programming a who’s online for leads at work, and was having lots of difficulty in getting the results I needed to properly display the most recent activity.

We have three tables that need to be looked up due to the way the schema was designed. The primary table is a user_log table that stores all actions that a user performed along with a timestamp. However, this log table does not include any user information aside from a user_id. So, to get the information about a user, we need to join that user_id to the user_id in a users table.

But, here’s where it gets really messed up – the users table doesn’t contain the information we need. Instead, it has a contact_id field in the users table, which must be then joined to a contacts table.

So it goes like this:

.. WHERE user_log.user_id = users.user_id AND users.contact_id = contacts.contact_id

Yeah, three tables just to get contact information.

I was finding that the standard SQL Distinct/Group By keywords were proving ineffective in getting the results I needed, which would be to just give me one user_id and the most recent created timestamp in the user_log table. Instead, I was getting multiple rows for the same user_id.

Fortunately, I found out about analytic functions in Oracle, which helped me isolate out unique user_ids and get the latest timestamp for that user by using ROW_NUMBER( ) where the sequence_number = 1 (giving me the topmost result for each user_id).

Unfortunately, I also learned that mySQL does not have analytic functions built in, but it can be emulated in native SQL (Part 1) (Part 2).

The best explanation I can give about the common aggregation functions that most people tend to use like SUM(), AVG(), etc vs analytic functions is:

Assume you have a set of rows.

The aggregation function will take that set of rows and output only one row with the results, while an analytic function will output that set of rows with the results appended.

The analytic functions do not have to work this way, but it’s useful for performing ranking analysis or sorting rows into individual sets of results.

Facelist Improved: Facebook Autocompletion JQuery Plugin

I was evaluating all the available JQuery plugins to get a Facebook-like contact autocompletion functionality, and deemed the one by Ian Tearle to be the most complete available. However, most complete does not mean fully done, or bug free, and I learned this the hard way doing an initial implementation on a project I’m doing at work.

Spending all day with the lovely Firebug debugger and testing in both Firefox 3 and IE7, I can say I have it working the way I need it to. My version fixes the following issues:

- Not all results that are returned are displayed

- Duplicate contacts can be inserted

- When searching, the user is not notified that a search is being performed

- The ID was being displayed with the search results, this has now been removed

There is still one lingering issue: the values stored in the hidden field… the plugin needs the trailing comma at the end of the values in order to properly perform backspace deletion. It’s really a minor issue as long as you remember in your code that parses the value, that when exploding the commas, you’ll have to ignore the final array row as it will be blank or null.

Demo

Download

I do not provide any support for my modified plugin, but feel free to leave comments if you hit bugs, and if I have time, I will fix them. I’m also hoping Ian will accept my changes so that I can just do a link to his page for the fix.

Ian Tearle’s Page for the Plugin

Edit: Yay, Ian accepted the changes, and they are now updated on the JQuery Plugin project page! I’ve updated my download link to point to the page.

Convert Oracle Date to UNIX Time

Oracle unfortunately does not have a UNIX conversion function. However, you can do it mathematically in SQL. I’ve written a function in PHP that will convert a date field to a UNIX timestamp in Oracle.

/**
* Inserts the SQL necessary to format a date to a UNIX time in Oracle.
* @param string Field name to convert.
* @param string (Optional) Field Alias
* @return string The SQL necessary to convert to UNIX time.
*/
function toUnixTime($field, $fieldAlias=null){
if($fieldAlias== null){
$fieldAlias= $field;
}

return "(ROUND((".$field." - to_date('01-01-1970', 'DD-MM-YYYY')) * (86400) + (28800)) ".$fieldAlias;
}

Usage:

$sql = “SELECT “.toUnixTime(“created”).” FROM users”;

or

//CreatedOn alias
$sql = “SELECT “.toUnixTime(“created”, “createdOn”).” FROM users”;

Working iCarousel Javascript Implementation

My work wanted an image carousel implemented into the website, and I could barely find any decent carousel implementations out there until I came across iCarousel by Fabio Zendhi Nagao.

I loved the examples on his website of how to use and implement it, but unfortunately the code examples do not work at all. Reading the comments section of the website, I came to realize that it’s not just me who can’t get this stuff to work the first time.

Anyways, after spending a few hours tinkering with it, I finally figured out how to do the horizontal example, and I’m placing a working package here so you can also download it if you’d like:

http://journal.suteki.nu/wp-content/uploads/2008/05/icarouselh.zip

Edit: I am not taking any requests to fix any part of iCarousel. Fabio’s scripts are completely broken; he should just take down his page because there is no indication that he cares about fixing his script at all.

Oracle, Propel 1.3 and Dates

It took me awhile to figure out how to parse an Oracle date type with Propel 1.3. The easiest way to do it would be this:

define(“DATE_FORMAT”, ‘M j, Y g:i a’);

< ?=date(DATE_FORMAT, $event->getStartDate(null)->format(“U”));?>

Assume there is an Event that stores the date and time of when the event begins. The code above basically translates the Oracle date to UNIX time, where you can then use the PHP date function to parse it out.

CLOB and PDO, specifically Oracle

So, my new job has me working with Oracle which is in my book, a way different beast compared to mySQL. Everything I know about databases from mySQL basically had to be re-tooled for Oracle.

I spent around 3 hours trying to figure out how to work in a set of prepared statements (one of which returned a value) in PHP PDO, and thought I might share how I did this.

Okay, so I have one stored procedure and one function.

PROCEDURE RETURN_CLOB(:CLOB_ID, :CLOB_OUT IN CLOB) — All this does was search a table with key/ CLOB value pairs. It basically selected a row with the :CLOB_ID, and :CLOB_OUT returned the CLOB value.

FUNCTION CONVERT_CLOB_TO_VARCHAR2(:CLOB_OUT IN CLOB) RETURN VARCHAR2 — this was a function definition that took in the :CLOB_OUT from RETURN_CLOB and spit it out as a VARCHAR2

Note that I did not write these two procedures, it’s something out of a package I used at work, but was having trouble trying to get them to work in PDO.

I’m using PHP Propel 1.3, but you can easily rewrite this in standard PDO calls, since the $con object is actually a combination of the PDO class and PDO Statement class.
Here’s the code:

$con = Propel::getConnection(‘…’); //All this does is just use PDO to create a connection
/*

The call below is the same as PDOStatement::prepare(). I’ve condensed the following into one line:

DECLARE

CLOB_Output CLOB;

BEGIN

PackageName.RETURN_CLOB( :Clob_Id, CLOB_Output );

:Str := PackageName.CONVERT_CLOB_TO_VARCHAR2( CLOB_Output );

END;

//So the declare part is to define an internal variable in Oracle, which I’ve named CLOB_Output. It is of type CLOB. The Begin … End block processes the statements in one container.

1. Return_CLOB returns the CLOB data in CLOB_Output

2. Call CONVERT_BLOB_TO_VARCHAR2 with CLOB_Output

3. The converted CLOB is returned into :Str
*/
$stmt = $con->prepare(“DECLARE CLOB_Output CLOB; BEGIN PackageName.RETURN_CLOB( :Clob_Id, CLOB_Output ); :Str := PackageName.CONVERT_CLOB_TO_VARCHAR2( CLOB_Output ); END;”);

$m = “”;

$stmt->bindValue(‘:Clob_Id’, ‘Clob_Id_goes_here’, PDO::PARAM_INT);

$stmt->bindParam(‘:Str’, $m, PDO::PARAM_STR, 32767); //replace 32767 with the size of the VARCHAR2 that you are returning from CONVERT_BLOB_TO_VARCHAR2

$stmt->execute();
//$m now contains the CLOB as a VARCHAR2!
Phew! Anyways, I’m going to say that Propel 1.3 isn’t ready for Oracle yet, and neither is PHP_PDO for Oracle. There’s too many hassles and tricks I had to use to do the things I needed in Oracle, and I’m going to recommend people continue to use the OCI8 extension.