NOTE: **use Perl;** is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

Ovid

(email not shown publicly)

http://publius-ovidius.livejournal.com/

**AOL IM:** ovidperl (**Add Buddy, Send Message**)

(email not shown publicly)

http://publius-ovidius.livejournal.com/

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Wednesday September 03, 2008

09:49 AM

I'd like to take a series of values and convert them to numbers between 0 and 255, but on a logarithmic scale. My math studies were a long time ago and for the life of me I can't remember how to do this. I'm hoping to take a 2D table of values and project them onto an HTML table in a 'heatmap' fashion.

Heatmap Ranges
More |
Login
| Reply

Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.

## scale linear then log (Score:0)

Maybe I'm misunderstanding, but I think you want to first scale logarithmically: log($val) if it's log base 'e'. Then linearly: so find the max value in the logged sequence, then multiply each value by 255 and divide by the max. Here's a kind of generic script, if I got it right:

## Re: (Score:1)

Need to scale linearly first in the case the minimum value is less than 1 before taking logs. (Also, $SCALE_MAX should be 255)

Instead, if you simplify the mathematics (and you know the minimum and maximum values), calculate the logarithmic 'scaling factor'

Then apply logarithmic scaling to each data element

So that the minimum value scales to zero, maximum value scales to 255. Adjust the linear scale in the log calculatio

## Log2 for heatmap, fun with pack (Score:1)

If you have such quantities of data that scaling and calling log repeatedly is a problem - and only if - there are old integer bitbang routines for log2 that could be done with XS or Inline::C or PDL.

Or you could (ab)use Perl 5.10 pack() to grab the floating point representation's exponent

Note that 256 buckets is a lot, hi res, for loglinear data unless it already was floating point or Math::BigFloat - as log2(MAXLONG) - log2(1) 256 or 8 bits -- it'

Bill

# I had a sig when sigs were cool

use Sig;