# Error Function

**URL:** <https://forum.kx.com/t/error-function/6322>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [October 23, 2009, 4:51pm UTC](https://forum.kx.com/t/error-function/6322 "2009-10-23T16:51:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamie\_Dumbill](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jamie\_Dumbill](https://forum.kx.com/u/Jamie_Dumbill)\
**Post date:** [October 23, 2009, 4:51pm UTC](https://forum.kx.com/t/error-function/6322/1 "2009-10-23T16:51:00Z")

</div>

Is there an error function in kdb as there is in C (supplied bymath.h)?[http://en.wikipedia.org/wiki/Error\_function](http://en.wikipedia.org/wiki/Error%5C_function)

---

<div class="post-metadata">

**Author:** ![aka\_magnus](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@aka\_magnus](https://forum.kx.com/u/aka_magnus)\
**Post date:** [October 27, 2009, 8:59pm UTC](https://forum.kx.com/t/error-function/6322/2 "2009-10-27T20:59:00Z")

</div>

User-Agent: G2/1.0  
X-Google-Token: vLK8JgwAAACjAuFVXenHeabL3k6CYR0B  
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686 (x86\_64); en-US;  
rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12 GTB6 GTBA,gzip(gfe),gzip(gfe)  
Message-ID: \<98be3673-f52b-41aa-862d-3834e8af4c9d@u16g2000pru.googlegroups.com\>  
Subject: Re: Error Function  
From: aka\_magnus \<niall.rea\>  
To: “Kdb+ Personal Developers”   
X-Google-Approved: [charlie@kx.com](mailto:charlie@kx.com) via web at 2009-10-27 21:00:20  
  
you’ve answered your question  
  
// qerf.c  
  
#include"k.h"  
  
K qerf(K v){  
 float v1=erf((float)v-\>f);  
 return kf(v1);  
}  
  
// gcc -fPIC -shared qerf.c -o erf.so -lm  
  
  
  
in q  
q)erf:(`$":./erf") 2:(`qerf;1)  
q)erf(1.1)  
0.8802051  
  
  
\</niall.rea\>

---

<div class="post-metadata">

**Author:** ![Attila](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Attila](https://forum.kx.com/u/Attila)\
**Post date:** [October 28, 2009, 9:57am UTC](https://forum.kx.com/t/error-function/6322/3 "2009-10-28T09:57:00Z")

</div>

see for example [http://www.gbkr.com/subjects/q/barrierdiffusion.html](http://www.gbkr.com/subjects/q/barrierdiffusion.html)

the erf function there can be vectorized and improved a bit

q)erfV:{1&(z\*y)\*1+sum prds neg[z\*z]\*/:x}[(%)over .5 1 1.5+:til  
42;2%sqrt acos -1]  
q)x:(-200000+til 100000)%100000  
q)erfV~erf each x  
1b  
q)\t erf each x  
353  
q)\t erfV x  
66

but using C is of course faster

K1(erfM){R xt!=-KF?krr(“type”):kf(erf(xf));}

q)erfM:`:./erf 2:`erfM,1  
q)\t erfM each x  
22  
q)erfV~erfM each x  
q)1b

if you vectorize the C function you can get even more speed

K1(erfMV){P(abs(xt)!=KF,krr(“type”))if(xt\<0)R kf(erf(xf));K r=ktn  
(KF,xn);DO(xn,kF(r)[i]=erf(xF[i]))R r;}

q)erfMV:`:./erf 2:`erfMV,1  
q)erfMV~erfM each x  
1b  
q)\t erfMV x  
9

Regards,  
Attila  
On 27 Oct 2009, at 20:59, aka\_magnus wrote:

\>  
\> you’ve answered your question  
\>  
\> // qerf.c  
\>  
\> #include"k.h"  
\>  
\> K qerf(K v){  
\> float v1=erf((float)v-\>f);  
\> return kf(v1);  
\> }  
\>  
\> // gcc -fPIC -shared qerf.c -o erf.so -lm  
\>  
\>  
\>  
\> in q  
\> q)erf:(`$":./erf") 2:(`qerf;1)  
\> q)erf(1.1)  
\> 0.8802051  
\>  
\>  
\>  
\>  
\> On Oct 23, 11:51 am, Jamie Dumbill \<jamie.dumb…\> wrote:  
\>\> Is there an error function in kdb as there is in C (supplied by  
\>\> math.h)?  
\>\>  
\>\> [http://en.wikipedia.org/wiki/Error\_function](http://en.wikipedia.org/wiki/Error_function)  
\>  
\> \>  
  
  
–Apple-Mail-24–756243888  
Content-Type: text/html  
  
  
 space; -webkit-line-break: after-white-space; “\>-span” face=3D"Monaco" size=3D"3"\>“font-size: 12px;”\>see for example q/barrierdiffusion.html"\>http://www.gbkr.com/subjects/q/barrierdiffusion.ht=  
ml

ize=3D"3"\>  
\<=  
/span\>

ze=3D"3"\>the er=  
f function there can be vectorized and improved a bit
iv\>=3D"Apple-style-span" style=3D"font-size: 12px;"\>  
iv\>\<=  
font class=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>pple-style-span" style=3D"font-size: 12px;"\>q)erfV:{1&(z\*y)\*1+sum prds =  
neg[z\*z]\*/:x}[(%)over .5 1 1.5+\:til 42;style-span" style=3D"border-collapse: separate; "\>e-span" face=3D"Monaco" size=3D"3"\>=3D"font-size: 12px;"\>2%sqrt acos -1e-span" style=3D"border-collapse: collapse; "\>an" face=3D"Monaco" size=3D"3"\>nt-size: 12px;"\>]

iv\>
lass=3D"Apple-style-span" style=3D"font-size: 12px;"\>q)x:(-200000+til 10000=  
0)%100000

ce=3D"Monaco" size=3D"3"\>e: 12px;"\>q)erfV[x]~erf each x

-style-span" face=3D"Monaco" size=3D"3"\>yle=3D"font-size: 12px;"\>1b

yle-span" face=3D"Monaco" size=3D"3"\>=3D"font-size: 12px;"\>q)\t erf each x

ass=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>yle-span" style=3D"font-size: 12px;"\>353

 class=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>-style-span" style=3D"font-size: 12px;"\>q)\t erfV x

\>"Apple-style-span" style=3D"font-size: 12px;"\>66

ont class=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>ple-style-span" style=3D"font-size: 12px;"\>  

nt class=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>le-style-span" style=3D"font-size: 12px;"\>  

iv\>
lass=3D"Apple-style-span" style=3D"font-size: 12px;"\>but using C is of cour=  
se faster

e=3D"Monaco" size=3D"3"\>: 12px;"\>  

=3D"Monaco" size=3D"3"\> 12px;"\>K1(erfM){R xt!=3D-KF?krr("type"):kf(erf(xf));}
\<=  
div\>=3D"Apple-style-span" style=3D"font-size: 12px;"\>  

=  
div\>
class=3D"Apple-style-span" style=3D"font-size: 12px;"\>q)erfM:`:./erf 2:`erf=  
M,1

onaco" size=3D"3"\>;"\>q)\t erfM each x

" face=3D"Monaco" size=3D"3"\>-size: 12px;"\>22

an" face=3D"Monaco" size=3D"3"\>nt-size: 12px;"\>q)erfV[x]~erfM each x

ass=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>yle-span" style=3D"font-size: 12px;"\>q)1b

ss=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>le-span" style=3D"font-size: 12px;"\>  

s=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>e-span" style=3D"font-size: 12px;"\>  

 class=3D"Apple-style-span" face=3D"Monaco" size=3D"3"\>-style-span" style=3D"font-size: 12px;"\>if you vectorize the C function you=  
 can get even more speed

-span" face=3D"Monaco" size=3D"3"\>"font-size: 12px;"\>  

span" face=3D"Monaco" size=3D"3"\>font-size: 12px;"\>K1(erfMV){P(abs(xt)!=3DKF,krr("type"))if(xt\<0)R kf(erf=  
(xf));K r=3Dktn(KF,xn);DO(xn,kF(r)[i]=3Derf(xF[i]))R r;}
\>
ss=3D"Apple-style-span" style=3D"font-size: 12px;"\>  

=  

s=3D"Apple-style-span" style=3D"font-size: 12px;"\>q)erfMV:`:./erf 2:`erfMV,=  
1

size=3D"3"\>q)er=  
fMV[x]~erfM each x

 face=3D"Monaco" size=3D"3"\>size: 12px;"\>1b

ce=3D"Monaco" size=3D"3"\>e: 12px;"\>q)\t erfMV x

yle-span" face=3D"Monaco" size=3D"3"\>=3D"font-size: 12px;"\>9

tyle-span" face=3D"Monaco" size=3D"3"\>e=3D"font-size: 12px;"\>  

yle-span" face=3D"Monaco" size=3D"3"\>=3D"font-size: 12px;"\>Regards,

-style-span" face=3D"Monaco" size=3D"3"\>yle=3D"font-size: 12px;"\> Attila

=  

e=3D"3"\>On 27 O=  
ct 2009, at 20:59, aka\_magnus wrote:
e-style-span" face=3D"Monaco" size=3D"3"\>tyle=3D"font-size: 12px;"\>  
=  
font\>

> =3D"Monaco" size=3D"3"\> 12px;"\>  
> you've answered your question  
>   
> // qerf.c  
>   
> #include"=  
> k.h"  
>   
> K qerf(K v){  
> float v1=3Derf((float)v-\>f);  
> &nbs=  
> p;return kf(v1);  
> }  
>   
> // gcc -fPIC -shared qerf.c -o erf.so -lm  
> \<=  
> br\>  
>   
> in q  
> q)erf:(`$":./erf") 2:(`qerf;1)  
> q)erf(1.1)  
> 0.880205=  
> 1

---

<div class="post-metadata">

**Author:** ![Jamie\_Dumbill](https://avatars.discourse-cdn.com/v4/letter/j/7bcc69/32.png) [@Jamie\_Dumbill](https://forum.kx.com/u/Jamie_Dumbill)\
**Post date:** [October 28, 2009, 11:08am UTC](https://forum.kx.com/t/error-function/6322/4 "2009-10-28T11:08:00Z")

</div>

Thanks guys, much appreciated, I didn’t know if this was alreadyimplemented.JamieOn Oct 28, 9:57?am, Attila Vrabecz \<attila.vrab…\> wrote:\> see for examplehttp://www.gbkr.com/subjects/q/barrierdiffusion.html\>\> the erf function there can be vectorized and improved a bit\>\> q)erfV:{1&(z_y)1+sum prds neg[zz]_/:x}[(%)over .5 1 1.5+:til ?\> 42;2%sqrt acos -1]\> q)x:(-200000+til 100000)%100000\> q)erfV~erf each x\> 1b\> q)\t erf each x\> 353\> q)\t erfV x\> 66\>\> but using C is of course faster\>\> K1(erfM){R xt!=-KF?krr(“type”):kf(erf(xf));}\>\> q)erfM:`:./erf 2:`erfM,1\> q)\t erfM each x\> 22\> q)erfV~erfM each x\> q)1b\>\> if you vectorize the C function you can get even more speed\>\> K1(erfMV){P(abs(xt)!=KF,krr(“type”))if(xt\<0)R kf(erf(xf));K r=ktn\> (KF,xn);DO(xn,kF(r)[i]=erf(xF[i]))R r;}\>\> q)erfMV:`:./erf 2:`erfMV,1\> q)erfMV~erfM each x\> 1b\> q)\t erfMV x\> 9\>\> Regards,\> ? ?Attila\> On 27 Oct 2009, at 20:59, aka\_magnus wrote:\>\>\>\>\>\> \> you’ve answered your question\>\> \> // qerf.c\>\> \> #include"k.h"\>\> \> K qerf(K v){\> \> ?float v1=erf((float)v-\>f);\> \> ?return kf(v1);\> \> }\>\> \> // gcc -fPIC -shared qerf.c -o erf.so -lm\>\> \> in q\> \> q)erf:(`$":./erf") 2:(`qerf;1)\> \> q)erf(1.1)\> \> 0.8802051\>\> \> On Oct 23, 11:51 am, Jamie Dumbill \<jamie.dumb…\> wrote:\> \>\> Is there an error function in kdb as there is in C (supplied by\> \>\> math.h)?\>\> \>\>[http://en.wikipedia.org/wiki/Error\_function](http://en.wikipedia.org/wiki/Error_function)\</jamie.dumb…\>\</attila.vrab…\>
