<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cat brain.log &#124; less &#187; php</title>
	<atom:link href="http://log.largevoid.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://log.largevoid.com</link>
	<description>Getting it down on `paper`</description>
	<lastBuildDate>Mon, 06 Feb 2012 06:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Parabolic SAR Calculation</title>
		<link>http://log.largevoid.com/2011/06/parabolic-sar-calculation/</link>
		<comments>http://log.largevoid.com/2011/06/parabolic-sar-calculation/#comments</comments>
		<pubDate>Sat, 04 Jun 2011 01:27:00 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Reference]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[stocks]]></category>
		<category><![CDATA[technical analysis]]></category>
		<category><![CDATA[technical indicators]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=542</guid>
		<description><![CDATA[Below you will find demonstrative code that calculates the Parabolic SAR indicator. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 [...]]]></description>
			<content:encoded><![CDATA[<p>Below you will find demonstrative code that calculates the Parabolic SAR indicator.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Computes the Parabolic SAR, as originally developed by Welles Wilder.
 *
 * @param array $his Extreme Points: The highest high
 * @param array $los Extreme Points: The lowest low
 * @param float $afInc Acceleration Factor Increment, for each time point
 *   Lower increment = less sensitive (indicitive)
 * @param float $afMax Acceleration Factor Max, rno matter how long the trend
 *   Lower max step = less sensitive (reactive)
 * @param bool $withDir Indicates if trend direction should be returned.
 * @return array 
 *  If $withDir is true, result is tuple of length 2.
 *    Tuple contains ( array of SAR values, array of trend direction )
 *  Else if $withDir is false, result is simply an array of SAR values.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> parabolicSAR<span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #339933;">,</span><span style="color: #000088;">$los</span><span style="color: #339933;">,</span><span style="color: #000088;">$afInc</span><span style="color: #339933;">=</span><span style="color:#800080;">0.02</span><span style="color: #339933;">,</span><span style="color: #000088;">$afMax</span><span style="color: #339933;">=</span><span style="color:#800080;">0.20</span><span style="color: #339933;">,</span><span style="color: #000088;">$withDir</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$los</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;First 2 parameters must be arrays: his, los&quot;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">E_USER_WARNING</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$withDir</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$los</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
   <span style="color: #990000;">trigger_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Arrays must be equal length.&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">E_USER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$withDir</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
   <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$withDir</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000088;">$keys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$his</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_values</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$los</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_values</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$los</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Initialize trend to whatever</span>
 <span style="color: #000088;">$trend</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;=</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Previous SAR: Use first data point&amp;#039;s extreme value, depending on trend</span>
 <span style="color: #000088;">$pSAR</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$trend</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Extreme point: Highest during uptrend || lowest during downtrend</span>
 <span style="color: #000088;">$EP</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$trend</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Acceleration factor</span>
 <span style="color: #000088;">$AF</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$afInc</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Initialize results based on trend guess</span>
 <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pSAR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// SAR Results</span>
 <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$trend</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// Trend Directions</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Compute &quot;tomorrow&quot; SAR</span>
 <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$n</span><span style="color: #339933;">=</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$n</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// Do for uptrend</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$trend</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
&nbsp;
     <span style="color: #666666; font-style: italic;">// Making higher highs: accelerate</span>
     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$EP</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$EP</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$AF</span> <span style="color: #339933;">=</span> <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$afMax</span><span style="color: #339933;">,</span><span style="color: #000088;">$AF</span><span style="color: #339933;">+</span><span style="color: #000088;">$afInc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Tomorrow&amp;#039;s SAR based on today&amp;#039;s price action.</span>
     <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pSAR</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$AF</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$EP</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$pSAR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Rule: SAR can never be above prior period&amp;#039;s low or the current low.</span>
     <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$nSAR</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$nSAR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Rule: If SAR crosses tomorrow&amp;#039;s price range, the trend switches.</span>
     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       <span style="color: #000088;">$trend</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$EP</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;">// set to the last EP recorded on the previous trend</span>
       <span style="color: #000088;">$EP</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// reset accordingly to this period&amp;#039;s maximum</span>
       <span style="color: #000088;">$AF</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$afInc</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// reset to its initial value of 0.02.</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// Do for downtrend</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Making lower lows: accelerate</span>
     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$EP</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       <span style="color: #000088;">$EP</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$los</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$AF</span> <span style="color: #339933;">=</span> <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$afMax</span><span style="color: #339933;">,</span><span style="color: #000088;">$AF</span><span style="color: #339933;">+</span><span style="color: #000088;">$afInc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Tomorrow&amp;#039;s SAR based on today&amp;#039;s price action.</span>
     <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pSAR</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$AF</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$EP</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$pSAR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Rule: SAR can never be below prior period&amp;#039;s highs or the current high.</span>
     <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$nSAR</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$nSAR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">// Rule: If SAR crosses tomorrow&amp;#039;s price range, the trend switches.</span>
     <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$trend</span> <span style="color: #339933;">=</span> <span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$nSAR</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$EP</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;">// set to the last EP recorded on the previous trend</span>
       <span style="color: #000088;">$EP</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$his</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// reset accordingly to this period&amp;#039;s maximum</span>
       <span style="color: #000088;">$AF</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$afInc</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;">// reset to its initial value of 0.02.</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end: if uptrend else downtrend</span>
&nbsp;
   <span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$keys</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$nSAR</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$d</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$keys</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$trend</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$pSAR</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$nSAR</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// END: for(i..n)</span>
&nbsp;
 <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$withDir</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span><span style="color: #000088;">$d</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// END: function parabolicSAR($his,$los,$afInc=0.02,$afMax=0.20)</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p><strong>Sources:</strong></p>
<ul>
<li><a href="http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:parabolic_sar">StockCharts.com: Chart School</a></li>
<li><a href="http://en.wikipedia.org/wiki/Parabolic_SAR">Wikipedia: Parabolic SAR</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2011/06/parabolic-sar-calculation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Files Ending in .php.html Unexpectedly Being Processed by the PHP Engine</title>
		<link>http://log.largevoid.com/2010/07/files-ending-in-php-html-unexpectedly-being-processed-by-the-php-engine/</link>
		<comments>http://log.largevoid.com/2010/07/files-ending-in-php-html-unexpectedly-being-processed-by-the-php-engine/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 18:14:29 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Gotchas]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[httpd.conf]]></category>
		<category><![CDATA[mod_php]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php.conf]]></category>
		<category><![CDATA[phpDocumentor]]></category>
		<category><![CDATA[syntax error]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=413</guid>
		<description><![CDATA[Apache/2.2.3 (Red Hat), PHP 5.1.6 and phpDocumentor v1.4.3 weren&#8217;t getting along. phpDocumentor was generating HTML with filenames of the form script.php.html. Apache&#8217;s mod_php wanted to interpret script.php.html as a PHP script and was throwing errors about improper formatting: PHP Parse error: syntax error, unexpected T_STRING in /var/www/default/docs/out/lib/lib/_php---obj---FFT.php.html on line 1, referer:... I wanted script.php.html to [...]]]></description>
			<content:encoded><![CDATA[<p>Apache/2.2.3 (Red Hat), PHP 5.1.6 and phpDocumentor v1.4.3  weren&#8217;t getting along.  phpDocumentor was generating HTML with filenames of the form <em>script.php.html</em>. Apache&#8217;s mod_php wanted to interpret script.php.html as a PHP script and was throwing errors about improper formatting:</p>
<p><code>PHP Parse error:  syntax error, unexpected T_STRING in /var/www/default/docs/out/lib/lib/_php---obj---FFT.php.html on line 1, referer:...</code></p>
<p>I wanted script<em>.php.html</em> to be treated as an HTML file and not be processed by the php engine.  This double-extension was causing me grief.  The docs claimed that only files <strong>ending </strong>in the extension will be handled by the AddHandler command.  I was incredulous of this claim, especially in light of Apache&#8217;s ability to do content negotiation with those <em>.var </em>language files.</p>
<p>Q: Why was this happening?  I first guessed it may have been due to mod_mime, but quickly ruled that out.   I then guessed it was the MultiView option&#8230; Maybe, this is one mechanism that governs content negotiation&#8230; The documentation claims:</p>
<pre># The index.html.var file (a type-map) is used to deliver content-
# negotiated documents.  The MultiViews Option can be used for the
# same purpose, but it is much slower.</pre>
<p>This statement lead me to suspect that my problem may actually be caused by an issue with the <em>AddHandler</em> option, since an AddHandler command must be executed to associate <code>type-map</code> with that <code>.var</code> extension.</p>
<p>Ah me, time is money, and in the end, I actually didn&#8217;t diagnose the problem, but I did discover (by trial and error) a <strong>workaround</strong>.</p>
<p>In file <em>/etc/httpd/conf.d/php.conf</em>, I surrounded</p>
<pre>AddHandler php5-script .php
AddType text/html .php
</pre>
<p>with</p>
<pre>&lt;Files ~ "\.php$"&gt;
  AddHandler php5-script .php
  AddType text/html .php
&lt;/Files&gt;</pre>
<p>Now, only files explicitly ending in &#8220;.php&#8221; will be treated as a php5-script.  Take that!</p>
]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2010/07/files-ending-in-php-html-unexpectedly-being-processed-by-the-php-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.1+ Date vs Strftime Timezone Annoyance</title>
		<link>http://log.largevoid.com/2010/02/php-date-strftime-timezone-annoyance/</link>
		<comments>http://log.largevoid.com/2010/02/php-date-strftime-timezone-annoyance/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 02:10:45 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Gotchas]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[putenv]]></category>
		<category><![CDATA[reverse compatable]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=302</guid>
		<description><![CDATA[In the old PHP4 days, you could change the default timezone by: putenv("TZ=America/Los_Angeles");. In php5, you can still do this and it&#8217;ll work for strtotime, strftime, gmstftime (maybe more). It will not work for methods date and gmdate. For those, starting in version 5.1, you&#8217;ll need to call date_default_timezone_set("America/Los_Angeles");. This is just another one of [...]]]></description>
			<content:encoded><![CDATA[<p>In the old PHP4 days, you could change the default timezone by: <code>putenv("TZ=America/Los_Angeles");</code>.  In php5, you can still do this and it&#8217;ll work for strtotime, strftime, gmstftime (maybe more).  It will not work for methods date and gmdate.  For those, starting in version 5.1, you&#8217;ll need to call <code>date_default_timezone_set("America/Los_Angeles");</code>.  This is just another one of those annoying things that make PHP a dying language.</p>
<p>Together, to be cross-version compliant:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">putenv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TZ=America/Los_Angeles&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;date_default_timezone_set&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
 <span style="color: #990000;">date_default_timezone_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;America/Los_Angeles&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We need both to be reverse-compatible with PHP4.</p>
]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2010/02/php-date-strftime-timezone-annoyance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The little known PHP htmlspecialchars</title>
		<link>http://log.largevoid.com/2010/02/the-little-known-php-htmlspecialchars/</link>
		<comments>http://log.largevoid.com/2010/02/the-little-known-php-htmlspecialchars/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 02:06:57 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Reference]]></category>
		<category><![CDATA[entities]]></category>
		<category><![CDATA[htmlentities]]></category>
		<category><![CDATA[htmlspecialchars]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=297</guid>
		<description><![CDATA[Want to replace only xml entities , &#038;? Don&#8217;t use htmlentites or str_replace; use htmlspecialchars. The only named entities for XML are &#38;amp;, &#38;gt; and &#38;lt;. For all others you need to use the Unicode character code (eg. &#38;#160;). Webkit: Entity ‘nbsp’ not defined – Convert HTML entities to XML]]></description>
			<content:encoded><![CDATA[<p>Want to replace only xml entities <, >, &#038;?  Don&#8217;t use htmlentites or str_replace; use <a href="http://us2.php.net/manual/en/function.htmlspecialchars.php">htmlspecialchars</a>.</p>
<p><q>The only named entities for XML are &amp;amp;, &amp;gt; and &amp;lt;. For all others you need to use the Unicode character code (eg. &amp;#160;).</q> <cite><a href="http://techtrouts.com/webkit-entity-nbsp-not-defined-convert-html-entities-to-xml/">Webkit: Entity ‘nbsp’ not defined – Convert HTML entities to XML</a></cite></p>
]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2010/02/the-little-known-php-htmlspecialchars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.3.1 Error Logging and Display of Said Errors</title>
		<link>http://log.largevoid.com/2010/02/php-5-3-1-error-logging-and-display-of-said-errors/</link>
		<comments>http://log.largevoid.com/2010/02/php-5-3-1-error-logging-and-display-of-said-errors/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 02:36:19 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Reference]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[display_errors]]></category>
		<category><![CDATA[error_log]]></category>
		<category><![CDATA[html_errors]]></category>
		<category><![CDATA[log_errors]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[win7]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=294</guid>
		<description><![CDATA[I just installed the PHP 5.3.1 Windows installer bundle [VC9 x86 Non Thread Safe (2009-Nov-19 09:53:39)] on my Windows 7 [Ultimate] machine. I want the PHP error output to get sent to stderr when run from the command line. I don&#8217;t want the errors logged to a file because I&#8217;m developing cli tools. Since we&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>I just installed the <a href="http://windows.php.net/downloads/releases/php-5.3.1-nts-Win32-VC9-x86.msi">PHP 5.3.1 Windows installer bundle [VC9 x86 Non Thread Safe (2009-Nov-19 09:53:39)]</a> on my Windows 7 [Ultimate] machine.  I want the PHP error output to get sent to stderr when run from the command line.  I don&#8217;t want the errors logged to a file because I&#8217;m developing cli tools.  Since we&#8217;re dealing with the cli, I also don&#8217;t want html errors.  Should be pretty easy&#8230; if you know how to edit the settings.</p>
<p>These worked for me:</p>
<pre>error_reporting = E_ALL
display_errors = stderr
display_startup_errors = On
log_errors = Off
html_errors = Off
;error_log = php-errors.log
</pre>
<p>Ensure that <strong>error_log is not set</strong>, otherwise error will not be displayed.  This problem <em>may</em> be related, but is obviously not, the <a href="http://bugs.php.net/bug.php?id=50839">date bug</a> mentioned in the PHP bug reports.  It may be fixed in the latest snapshot.  As a note, the php.ini date.timezone setting is <code>date.timezone = "Pacific/Honolulu"</code>, and I still had this problem.</p>
<p>Other things I noticed:<br />
When error_log = syslog, you can find the PHP events in the windows Event Viewer -> Windows Logs -> Application.  I mention it in case you want that type of thing&#8230; or think error that logging may not be working.</p>
]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2010/02/php-5-3-1-error-logging-and-display-of-said-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP/GD Bug: imageFilledEllipse Centered on Y=0</title>
		<link>http://log.largevoid.com/2009/11/phpgd-bug-imagefilledellipse-centered-on-y0/</link>
		<comments>http://log.largevoid.com/2009/11/phpgd-bug-imagefilledellipse-centered-on-y0/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 23:04:43 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Scripts, Tricks and Hacks]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[imageFilledEllipse]]></category>
		<category><![CDATA[imageFilledRectangle]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=145</guid>
		<description><![CDATA[I was drawing circles using PHP&#8217;s GD function imageFilledEllipse. I noticed that when I went to draw a circle having its center at $y=0,there would be a line missing at $y=1. Here&#8217;s the work-around: if&#40; -1 &#60;= $py &#38;&#38; $py &#60;= 1 &#41; &#123; // Undocumented PHP bug when Y coordinate is zero. imageFilledEllipse&#40;$img,$px,$py,$X,$X,$color&#41;; imageFilledRectangle&#40;$img,$px-$halfX,$py-1,$px+$halfX,$py+1,$color&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>I was drawing circles using PHP&#8217;s GD function <a href="http://us3.php.net/manual/en/function.imagefilledellipse.php"><em>imageFilledEllipse</em></a>.  I noticed that when I went to draw a circle having its center at $y=0,there would be a line missing at $y=1.</p>
<p>Here&#8217;s the work-around:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$py</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$py</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// Undocumented PHP bug when Y coordinate is zero.</span>
  <span style="color: #990000;">imageFilledEllipse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span><span style="color: #000088;">$px</span><span style="color: #339933;">,</span><span style="color: #000088;">$py</span><span style="color: #339933;">,</span><span style="color: #000088;">$X</span><span style="color: #339933;">,</span><span style="color: #000088;">$X</span><span style="color: #339933;">,</span><span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">imageFilledRectangle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span><span style="color: #000088;">$px</span><span style="color: #339933;">-</span><span style="color: #000088;">$halfX</span><span style="color: #339933;">,</span><span style="color: #000088;">$py</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$px</span><span style="color: #339933;">+</span><span style="color: #000088;">$halfX</span><span style="color: #339933;">,</span><span style="color: #000088;">$py</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">imageFilledEllipse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span><span style="color: #000088;">$px</span><span style="color: #339933;">,</span><span style="color: #000088;">$py</span><span style="color: #339933;">,</span><span style="color: #000088;">$X</span><span style="color: #339933;">,</span><span style="color: #000088;">$X</span><span style="color: #339933;">,</span><span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>PHP Version:</p>
<pre>PHP 4.3.9 (cgi) (built: Apr  1 2009 10:41:42)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies</pre>
<p><code>$ yum search php-gd</code></p>
<pre>
php-gd.i386                              4.3.9-3.26             installed
Matched from:
php-gd
The php-gd package contains a dynamic shared object that will add
support for using the gd graphics library to PHP.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2009/11/phpgd-bug-imagefilledellipse-centered-on-y0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Log Math to Solve Iterative Problems</title>
		<link>http://log.largevoid.com/2009/11/using-log-math-to-solve-iterative-problems/</link>
		<comments>http://log.largevoid.com/2009/11/using-log-math-to-solve-iterative-problems/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 01:09:36 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Reference]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[earth]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[mercator]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[projection]]></category>
		<category><![CDATA[viewport]]></category>
		<category><![CDATA[zoom]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=134</guid>
		<description><![CDATA[Premise: You have a map of infinite resolution, but finite viewing space. You have points on that map that have a fixed foot print. Question: How much do you need to zoom before the points are clearly distinguishable? Let us arbitrarily define the criteria for distinguishability as an icon of 25 pixels in size. This [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Premise</strong>: You have a map of infinite resolution, but finite viewing space.  You have points on that map that have a fixed foot print.</p>
<p><strong>Question</strong>: How much do you need to zoom before the points are clearly distinguishable?</p>
<p>Let us arbitrarily define the <em>criteria for distinguishability</em> as an icon of 25 pixels in size.  This icon, it can be said, has a radius of 13 pixels.  In reality, icon size is a function of your viewport&#8217;s size.  I leave that up to you to calculate.</p>
<p>Let us define the <em>scale of the map</em> as the absolute width of the map (in map units) divided by the width of the view port.  As an example, let&#8217;s use the Earth&#8217;s longitude range: -180 to +180 degrees, but in a global Mercator projection.  The coordinate range of longitude for this map is [-20037508,+20037508) meters.  Our viewport has a range of [0,255] pixels (viewport units).  The scale of this map in this viewport is (2*20037508)/256.  Be careful not to double-count +20037508, as -20037508 = +20037508 and would yield an improper scale.  The viewport size is an integer and does not wrap; there are no fractions of a pixel in a viewport.</p>
<p>We shall now define a <em>target scale</em>.  The target scale is the desired scale of the map that allows our icons to become viewable.  Our example will use buildings having a foot print of 300 meters (map units) in width and depth.  Our target scale, therefore, is 300/25 = 12 meters per pixel.</p>
<p><strong>Question</strong>: How much do we need to zoom in before our buildings are distinguishable?</p>
<p><strong>Implementation</strong> 1 (naive, iterative):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> zoomLevel<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$maprange</span><span style="color: #339933;">,</span> <span style="color: #000088;">$viewport</span><span style="color: #339933;">,</span> <span style="color: #000088;">$distFactor</span><span style="color: #339933;">=</span><span style="color:#800080;">0.10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$zoomFactor</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// $viewport = 256;</span>
  <span style="color: #666666; font-style: italic;">// $maprange = 2*M_PI*6378137;</span>
  <span style="color: #000088;">$scale</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$maprange</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$viewport</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// 10% of viewport required to distinguish an icon.</span>
  <span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$distFactor</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$viewport</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$zoom</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$scale</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$target</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$zoom</span> <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$scale</span> <span style="color: #339933;">/=</span> <span style="color: #000088;">$zoomFactor</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$zoom</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Implementations</strong> 2, 3 and 4:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> zoomLevel<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$maprange</span><span style="color: #339933;">,</span> <span style="color: #000088;">$viewport</span><span style="color: #339933;">,</span> <span style="color: #000088;">$distFactor</span><span style="color: #339933;">=</span><span style="color:#800080;">0.10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$zoomFactor</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$scale</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$maprange</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$viewport</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$distFactor</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$viewport</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// naive log</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$scale</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$zoomFactor</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$zoomFactor</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// commutative log</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$scale</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$zoomFactor</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// subtraction property of logs (best implementation)</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$scale</span><span style="color: #339933;">/</span><span style="color: #000088;">$target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$zoomFactor</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2009/11/using-log-math-to-solve-iterative-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compiling Datascope.so for PHP</title>
		<link>http://log.largevoid.com/2008/10/compiling-datascope-so-for-php/</link>
		<comments>http://log.largevoid.com/2008/10/compiling-datascope-so-for-php/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 10:46:56 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[antelope]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[datascope]]></category>
		<category><![CDATA[dl]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.largevoid.com/?p=21</guid>
		<description><![CDATA[Does the following sentence make sense? In order to be able to simply dl(&#8220;Datascope.so&#8221;) in your PHP code, you first need to make sure Datascope.so is compiled and copied to the extension_dir, as specified in php.ini (ex: /usr/lib/php4). Answer: No: Stop right now. You&#8217;re not qualified to read this article. Yes: Keep reading. Notes You&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>Does the following sentence make sense?</p>
<p>In order to be able to simply dl(&#8220;Datascope.so&#8221;) in your PHP code, you first need to make sure Datascope.so is compiled and copied to the <strong>extension_dir</strong>, as specified in <em>php.ini</em> (ex: <em>/usr/lib/php4</em>).</p>
<p><strong>Answer:</strong><br />
  No: Stop right now.  You&#8217;re not qualified to read this article.<br />
  Yes: Keep reading.</p>
<h3>Notes</h3>
<p>You&#8217;re not compiling the PHP source code for actual use, so <strong>please</strong> don&#8217;t install it.  If you do, you run the risk of clobbering any pre-existing php installations, and overwriting previously compiled-in modules.  The only reason why you &#8221;make&#8221; PHP is so that the Zend libraries are properly initialized.  </p>
<p>The PHP &#8221;make&#8221; step may be avoidable, but given that the compile doesn&#8217;t affect system performance, I didn&#8217;t bother.  </p>
<p>Paths should be changed to reflect your setup.  If you don&#8217;t have the src/contrib/data/php/datascope code, you can get it from the <a href="http://www.indiana.edu/~aug/source_downloads/download.html">contrib source</a> at <a href="http://www.brtt.com/">BRTT</a>&#8216;s web site.</p>
<p>Contrib appears to be maintained by <a href="http://www.indiana.edu/~aug/">Indiana University</a>.</p>
<h3>Paths</h3>
<ul>
<li><strong>Antelope Bash Script</strong> &#8221;/opt/antelope/4.9/setup.sh&#8221;</li>
<li><strong>Datascope Source</strong> &#8221;/opt/antelope/4.9/src/contrib/data/php/datascope&#8221;</li>
<li><strong>PHP Source Code URL</strong> &#8221;http://downloads.php.net/ilia/php-5.1.4.tar.bz2&#8221;</li>
<li><strong>PHP Modules Directory</strong> &#8221;/usr/lib/php/modules/&#8221;</li>
</ul>
<h3>Compile and Install Steps</h3>
<ol>
<li>wget <em>&lt;PHP Source Code&gt;</em></li>
<li>bunzip2 php-5.1.4.tar.bz2</li>
<li>tar xf php-5.1.4.tar</li>
<li>cd php-5.1.4</li>
<li>PHP_INC=`pwd`</li>
<li>export PHP_INC</li>
<li>./configure</li>
<li>make</li>
<li>cd <em>&lt;Datascope Source&gt;</em></li>
<li>source <em>&lt;Antelope Bash Script&gt;</em></li>
<li>sudo make install</li>
<li>sudo cp Datascope.so <em>&lt;PHP Modules Directory&gt;</em></li>
</ol>
<h3>PHP Script Example</h3>
<p>Proper usage of the &#8221;Datascope.so&#8221; module follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">putenv</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;PFPATH=/opt/antelope/4.9/data/pf&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">putenv</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;ANTELOPE=/opt/antelope/4.9&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>PATH_SEPARATOR<span style="color: #339933;">.</span><span style="color: #990000;">getenv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ANTELOPE'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/data/php&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">extension_loaded</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Datascope.so&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #990000;">dl</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Datascope.so&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Failed to load datascope.so&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://log.largevoid.com/2008/10/compiling-datascope-so-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  log.largevoid.com/tag/php/feed/ ) in 1.12917 seconds, on Feb 8th, 2012 at 3:46 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 8th, 2012 at 4:46 am UTC -->
