# Math Functions???

### [1/8] from: robert::lancaster::opennw::com at: 9-May-2001 9:15

A wee while ago some one posted a group of math functions.... These mimic
some kind of math language APL... ACR... (I can't remember the exact
name).... Does anyone remember what it was called so I can go look them up
on the list archive???
[ Conveniently a a convolve function would be very handy ;-> ]
Thanks
Rob Lancaster.

### [2/8] from: brian:hawley at: 10-May-2001 12:31

Hi!
Robert Lancaster wrote:

>A wee while ago some one posted a group of math functions.... These mimic
>some kind of math language APL... ACR... (I can't remember the exact
>name).... Does anyone remember what it was called so I can go look them up
>on the list archive???

APL. The last message was from me and had the subject:
[REBOL] Re: Thanks for the warm welcome!
There were only sum, product and vector iota functions.
Earlier, someone posted some bignum math code, but that
might not be what you're looking for.

>[ Conveniently a a convolve function would be very handy ;-> ]

I'm not familiar with that term. What does a convolve
function do? Perhaps I could help...
Brian Hawley

### [3/8] from: robert:lancaster:opennw at: 11-May-2001 11:05

Thanks for the reply Brian....
So you want to know what convolve is.... Hmmm.....
The best way to decribe it is as a function that can be used to mulitply two
audio samples together. However it is used for a far more than this.... A
2-D dimensional convolve can be used for Blur and sharpen operations in a
Paint Program.
Basic 1-d operation...is ( if I remember it correctly )
a: [ 2 4 6 8 10 12 14 18 20 22 ]
b: [ 1 3 5 ]
B will now be "slid" through A. Any place where A aligns with a value in B
will be multiplied and added to a sum. The resultant sum will be appended
into an array.
First Interation
[ 2 4 6 8 10 12 14 18 20 22 ]
[ 1 3 5 ]
= 2 * 5
Resulting Array = [ 10 ]
2nd Interation
[ 2 4 6 8 10 12 14 18 20 22 ]
[ 1 3 5 ]
= 5*4 + 3*3
Resulting Array = [ 10 29 ]
3rd Interation
[ 2 4 6 8 10 12 14 18 20 22 ]
[ 1 3 5 ]
= 6*5 + 3*4 + 1*2
Resulting Array = [ 10 29 36 ]
The Last iteration will be
[ 2 4 6 8 10 12 14 18 20 22 ]
[ 1 3 5 ]
= 1*22
Resulting Array = [ 10 29 36 XX XX XX ... ... ... 22]
This carries on until B has 'slid' entirely through A. The resulting arrary
will be ( length? A + ( 2 * length? b) long.
I was not looking forward to implementing this because of the level of
nested for loops required. However in describing it on paper I think i've
figured out an easy way to complete the task. Not very effecient mind...
Thanks
Rob. Lancaster.

### [4/8] from: gjones05:mail:orion at: 10-May-2001 18:47

From: "Robert Lancaster"
<major snip>

> However in describing it on paper I think i've
> figured out an easy way to complete the task. Not very effecient

mind...

> Thanks
>
> Rob. Lancaster.

That's "talking to the duck." --as found in The Pragmatic Programmer.
The authors recommend talking it out when you've reached a hitch.
However, psychiatrists also recommend this technique, so it may be a
wash. <grin>
--Scott Jones

### [5/8] from: gjones05:mail:orion at: 11-May-2001 6:55

From: "Robert Lancaster"

> oooouuhhh... Ta!
>
> My method was going to prove very ineffecient... It involved insertion

of

> zeros to pad the arrays... ( very lazy programming... ) Mind you I

wasn't

> going to try and apply it large arrays.
>
> Thanks...
>
> Rob Lancaster.
>
> (p.s Is replying to your own E-mails tantamount to talking to ones

self)
Hi, Rob,
You saved me a lot of time this morning. Lying in bed last night I was
thinking that maybe padding one of the arrays with zeros ... I was
feeling just too plain lazy to get back up and try it. So being just
plain lazy actually saved me time! ;-)
By the way, I've been known to send myself e-mails as reminders, thereby
convincing myself that I am in the early stages of Alzheimer's.
--Scott Jones

### [6/8] from: larry:ecotope at: 10-May-2001 19:57

Hi Robert

>[ Conveniently a a convolve function would be very handy ;-> ]
> I was not looking forward to implementing this because of the level of
> nested for loops required. However in describing it on paper I think i've
> figured out an easy way to complete the task. Not very effecient mind...

The script below implements a simple convolution function. End values are
dropped, but you can pad the data block with zero's or modify the function
to do that. When large arrays are convolved, dropping the end values is
common. Some examples using Savitsky-Golay filters:

>> do %convolve.r

Script: "Convolution" (10-May-2001)
;the data to be processed

>> a: [1 2 3 4 5 4 3 2 1]

== [1 2 3 4 5 4 3 2 1]
; a convolution kernel for simple smoothing

>> b: [.25 .50 .25]

== [0.25 0.5 0.25]

>> convolve a b

== [2 3 4 4.5 4 3 2]
; a convolution kernel for first differences (numerical differention)

>> b: [1 -1]

== [1 -1]

>> convolve a b

== [-1 -1 -1 -1 1 1 1 1]
As you can see below, writing numerical code in REBOL is pretty easy, in
fact the code is not much different than in other languages. But REBOL,
being an interpreter, tends to be very slow for large arrays.
HTH
-Larry
-------------------code--------------------------
REBOL [
Title: "Convolution"
Author: "Larry Palmiter"
File: %convolve.r
Version: 0.1.0
Date: 10-May-2001
Purpose: {A simple convolution function}
Comment: {
End values are dropped (one of a variety
of possible ways to treat the ends, i.e.
circular convolution, pad with 0, etc.)
}
]
convolve: func [data [block!] filter [block!] /local k m n out] [
n: length? data m: length? filter
if m > n [exit]
out: copy []
repeat j n - m + 1 [
x: 0
k: j
repeat i m [
x: data/:k * filter/:i + x
k: k + 1
]
append out x
]
out
]

### [7/8] from: larry:ecotope at: 10-May-2001 20:20

Replying to my own message.
Oops, I left x out of the locals, the func spec block should be:
[data [block!] filter [block!] /local k m n x out]
-Larry

### [8/8] from: robert:lancaster:opennw at: 11-May-2001 15:28

oooouuhhh... Ta!
My method was going to prove very ineffecient... It involved insertion of
zeros to pad the arrays... ( very lazy programming... ) Mind you I wasn't
going to try and apply it large arrays.
Thanks...
Rob Lancaster.
(p.s Is replying to your own E-mails tantamount to talking to ones self)