Get Parent Cell Reference / Transitive Property - java

I'm having a problem where I need to reference the parent cell. So say A1's formula is "=B1", and B1's formula is "=C1". I need to compress A1's formula down to "=C1". I'm not sure if there is a way to do this in excel, or if there is a way to do this Apache POI. I have looked around, but can't really seem to find a solution to this. Does anyone know how to do this in excel or with the POI api?

In your Sample running this would give you the results you asked for.
Sub GetLastPrecedent()
Dim pres As Range
Dim TestCell As Range
Set TestCell = Range("A1")
'Set pres to all cells that are used in getting the value of TestCell
'This includes all precedents of precedents
Set pres = TestCell.Precedents
'This will return the absolute precedent of the ones returned
Set pres = pres.Cells(pres.Rows.Count, pres.Columns.Count)
'This will set the formula in TestCell to use the absolute address
TestCell.Formula = "=" & pres.Address
End Sub
I hope this can at least help guide you to what you are looking for. More info will result in a better answer. Remember if you have complex formula that reference many cells this will become very dangerous and complicated. I only provide this sample based on the information given as a way to help guide you.

Related

Write excel formula using Java Apache POI

I've been searching around a lot for this but couldn't find a solution. Hope someone can help me here.
I am using Apache POI to create a simple tool. Formulas will be inputted by user, and the result will be written on file. Im stuck at extending/filling the formula.
Suppose you have a simple excel formula:
IF(A2=B2,True,False)
A drag downwards on excel would result in:
IF(A2=B2,True,False)
IF(A3=B3,True,False)
IF(A4=B4,True,False)
IF(A5=B5,True,False)
. .
.
Now I want to do the same in my program. I will know which row to end. i just can't get the row index to increment. I have already done it manually (writing formula in program), but now I need to use this when formula is passed by user.
I have something like this for the manual part:
for (int ind = 2; ind < rownumb ; ind++)
{
sheet.getRow(i).createCell(12).setCellFormula("IF(K" + ind + "=L" + ind + ",FALSE,TRUE)");
}
Now the user will input:
=IF(K=L, False, True) OR =IF(K0=L0, False, True)
I want to add this formula auto-filling (incrementing) all the way till rownumb. How can I do this?
If there is no direct way, can someone suggest some other approach, however the requirement is that the formula will be passed by user.
Thanks.
Excel does not store it's function "in a sheet's cell" but as a VBA function. In order to be able to properly utilize excel function yourself you will need to emulate that. Check this article about user defined functions, this might get you going.

java.math.BigDecimal.scale() equivalent for double

I've got a matrix of values like the one below that I need to scale. I've been looking around for an inbuilt function if there is one that could do this for me. I haven't found one & so have ended up writing code to do the scaling using the below formula
scaledMatrix = (Matrix - MeanMatrix)/Standard Deviation
This code is a bit buggy & I'm working on correcting it. While I do that, I happened to bump on java.math.BigDecimal.scale() & did look up an equivalent for double as the matrix I have is double type numbers
If someone could please help me with details on
1) If there is an inbuilt function that accepts matrix of values & returns me the scaled matrix
2) `java.math.BigDecimal.scale()` equivalent for `double` type data
Any help would be much appreciated please.
The BigDecimal.scale() method does not do what you seem to think it is doing. A BigDecimal value is stored as a * 10^b (where ^ denotes exponentiation). The BigDecimal.scale() method basically returns the b part of that.
I do not know of a similar method for double values, nor do I know of a method which performs the function you need. Since you put apache-commons in the tags, I suggest you look into Apache Commons's extensive statistical library.

Find ZIP Code from Latitude and Longitude

I know there are web services out that have this information, however they can be limited to per day requests. I have about 114,000 records I need zip codes for. I have a data base full of zip codes with there lat and longs. However I am not sure how I can calculate the given lat and long against the zip code lat and long.
Basically I need to cross reference the given address lat and long against the supplied zip code lat and long. I can either use PHP, Java, or MySQL Procedure or just a calculation.
I can give you a stepping stone but thats about it, in this case.
$distance = 10;
$latitude = 37.295092;
$longitude = -121.896490;
$sql = "SELECT loc.*, (((acos(sin(($latitude*pi()/180)) * sin((`latitude`*pi()/180))+cos(($latitude*pi()/180)) * cos((`latitude`*pi()/180)) * cos((($longitude - `longitude`)*pi()/180))))*180/pi())*60*1.1515) AS `distance` FROM table_with_lonlat_ref loc HAVING distance < $distance"
if you create a query that does a JOIN between the 2 tables you have and reduce the distance to 1 or 2, you could in concept come up with just about all the lat/lon combinations you need. Or you could also find a DB that has all the US zipcodes, that also has lat/lon then query over one table to insert into another based on the matched zipcodes. I have such a zipcode DB somewhere.
also might I suggest http://www.maxmind.com/app/geolite its never complete less you wanna pay for it and it changes up every so often but. From this you can get almost nearly every combination of lat/lon possible to use as your reference point based on IP of a visitor (its off a little in some cases as the IP may steam from a hub a town or 2 away. But its better than nothing, gives you only limits your server can handle, and no worry about API restrictions outside of usage terms from maxmind.
Anyway all in all, Ive been using this combination for a while on a number of sites and have yet to come up with much problems to date. Well I know its not a direct answer to your question but I hope it leads you to a solution
Since you already have a database of lats and longs, I'm going to assume it describes a set of rectangular regions from (latA, lonA) to (latB, lonB), each with an associated zip code. I'll also assume (or recommend) that you've indexed those four fields.
Your query can fairly easily match whether a coordinate (coordA, coordB) fits within the two ranges describing that rectangle.
update coords set zip_code=(
select zip_code from zip_codes
where coords.coordA >= zip_codes.latA
and coords.coordA <= zip_codes.latB
and coords.coordB >= zip_codes.lonA
and coords.coordB <= zip_codes.lonB
limit 1
)
Caviat: You should verify whether latA < latB and lonA < lonB in your zip code database, and verify that you're using the same coordinate system in both tables. You may need to make adjustments, either through a conversion or by changing the operators appropriately.

How would I construct a really simple java maths expression to be used with KNIME

Im currently using a program called KNIME, which is used for analysing data. For some of my data, I want each row in a column to be averaged with the value in the previous row. The 'java snippet' option requires a 'global value declaration' and a 'method body'. The column name is 'new acc'.
I understand to use this program more efficiently I'll probably need to learn simple java (and its on my to do), but just for this evening I would like a quick check on some of the data used.
Any help is really appreciated - ive attached an image of the layout.
Thanks!
If you aren't required to use the Java Snippet, I'd recommend the Math Formula node.
There's a Moving Average Node which might be suitable for the task.
What about putting
double acc = Double.NaN;
to the global area, and something like this to the method body:
if (Double.isNaN(acc)) {
acc = $z$;
return $z$;
} else {
double avg = (acc + $z$) / 2;
acc = $z$;
return avg;
}
As a partial answer to the one from Sylvansight, it should be noted that the Java Snippet node is executed on a per row basis, so it's not even possible to use the Java Snippet node to access the values in the previous or subsequent rows.
Math formula node fits better your problem, but if you want to use **java snippet node (simple) ** just put the formula in the return (using normal java sintax). return 1+9;

Fuzzy Queries in Lucene

I am using Lucene in JAVA and indexing a table in our database based on company name. After the index I wish to do a fuzzy match (Levenshtein distance) on a value we wish to input into the database. The reason is that we do not want to be entering dupes because of spelling errors.
For example if I have the company name "Widget Makers XYZ" I don't want to insert "Widget Maker XYZ".
From what I've read Lucene's fuzzy match algorithm should give me a number between 0 and 1, I want to do some testing and then determine and adequate value for us determine what is valid or invalid.
The problem is I am stuck, and after searching what seems like everywhere on the internet, need the StackOverflow community's help.
Like I said I have indexed the database on company name, and then have the following code:
IndexSearcher searcher = new IndexSearcher(directory);
new QueryParser(Version.LUCENE_30, "company", analyzer);
Query fuzzy_query = new FuzzyQuery(new Term("company", "Center"));
I encounter the problem afterwards, basically I do not know how to get the fuzzy match value. I know the code must look something like the following, however no collectors seem to fit my needs. (As you can see right now I am only able to count the number of matches, which is useless to me)
TopScoreDocCollector collector = TopScoreDocCollector.create(10, true);
searcher.search(fuzzy_query, collector);
System.out.println("\ncollector.getTotalHits() = " + collector.getTotalHits());
Also I am unable to use the ComplexPhraseQueryParser class which is shown in the Lucene documentation. I am doing:
import org.apache.lucene.queryParser.*;
Does anybody have an idea as to why its inaccessible or what I am doing wrong? Apologies for the length of the question.
You do not need Lucene to get the score. Take a look at Simmetrics library, it is exceedingly simple to use. Just add the jar and use it thus:
Levenstein ld = new Levenstein ();
float sim = ld.GetSimilarity(string1, string2);
Also do note, depending on the type of data (i.e. longer strings, # whitespaces etc.), you might want to look at other algorithms such as Jaro-Winkler, Smith-Waterman etc.
You could use the above to determine to collapse fuzzy duplicate strings into one "master" string and then index.
You can get the match values with:
TopDocs topDocs = collector.topDocs();
for(ScoreDoc scoreDoc : topDocs.scoreDocs) {
System.out.println(scoreDoc.score);
}

Categories

Resources