Target audience: Intermediate
Estimated reading time: 15'
This post describes the definition and an implementation of the functions vector.
Overview
Space of functions are commonly used in machine learning (kernel functions) and differential geometry (tensors and manifolds).The most common technique to create a functions space is to define the space as a category and assign monadic operations to it.
This post discusses an alternative, less elaborate approach to define a functions space as a container. The key operator on function is the composition as defined in the Function1[-T, +R] class:
This post discusses an alternative, less elaborate approach to define a functions space as a container. The key operator on function is the composition as defined in the Function1[-T, +R] class:
def andThen[A](g: (R) ⇒ A): (T) ⇒ A
def compose[A](g: (A) ⇒ T): (A) ⇒ R
Let's use andThen as the basic block to build our functions space.
The andThen is a member method of some of the Scala standard library collections such as List and HashMap.
val xsInt = List[Int](4, 0, 9, 56, 11) xsInt.andThen((n: Int) => n*n).mkString(",") // print 16,0,81,3136,121
As mentioned in the introduction, the Function1 implements andThen to compose two functions as this o otherFunction where the o composition operator is defined as
f o g: (t: T) => f(g(t))The method is implemented using the andThen method
val fg = Math.sin andThen (x: Double) => x*x Console.println(s"fg(5.0): ${fg(5.0)}")
Function vectors space
References
Scala By Example - M. Odersky - June 2014
A function space is defined as a space of function vectors, similar to Euclidean space for which variables is defined as a vector or array of real values. A function vector is commonly used to convert a vector or tensor from one space to another non-euclidean space by transforming the original coordinates (x,y,z) to another coordinates in a different space (f(x,y,z), ..) using the function vector.
Let's implement the functions vector as the class FunctionVector (line 1) and its two most important method, composition (line 2) and the dot product (tensor product) (line 3).
1 2 3 4 5 6 | class FunctionVector[T](fVec: List[T=>T]) { def andThen(op: T => T): T=>T def dot(opVec: List[T=>T])(implicit num: Numeric[T]): T => T def map(op: T => T): List[T=>T] ... } |
The dot product is defined as the product of two vectors functions. For example in a function vector spaces of dimension 3, v(f,g,h) and v'(f',g',h')
dot: (x,y,z) -> f o f'(x,y,z) + g o g'(x,y,z) + h o h'(x,y,z)The method andThen composes this function vector with a function op and generates a 'cumulative' composed function as
f o g o h o opThe iteration along the function vector is implemented as a tail recursion. The method relies on the List[T=>T].andThen method (line 6).
1 2 3 4 5 6 7 8 9 | def andThen(g: T => T): T => T = { @scala.annotation.tailrec def andThen(xsf: List[T=>T])(op: T => T): T => T = if(xsf.size == 0) op else andThen(xsf.drop(1))(xsf.head andThen op) andThen(fVec.reverse)(op) } |
The 'dot' product takes another function vector of same size as argument. The two vectors are zipped to generate a Iterable[T=>T] vector of composed function elements using a map. Finally the resulting dot function is computed by generating a new function with summation of the elements of the composed functions vector. The summation requires the instance of Numeric to be declared as an implicit argument.
def dot(gVec: List[T=>T])(implicit num: Numeric[T]): T => T = { val composed = fVec.zip(opVec).map(fg => fg._1.andThen(fg._2)) (t: T) => composed.map( _(t)).sum }
Contrary to the andThen method, the map convert a function vector to another function vector by applying a natural transformation.
// return List[T => T] def map(op: T => T) = fVec.map( _ andThen op)
Finally, the definition of the dot product can be extended to any aggregation method aggr
1 2 3 4 5 6 7 8 9 10 11 12 | def dot( opVec: List[T=>T], aggr: (T =>T, T=>T) => (T=>T)): T => T = { val composed = fVec.zip(opVec).map( fg => fg._1 andThen fg._2 ) (t: T) => composed./:((t: T) => t)( (h,f) =>aggr(h,f) )(t) } |
The aggregation method passed as argument of the dot method (line 3) is a simplified version of the aggregation defined in the Scala standard library. It is used to implement the dot method on the composed functions vectors (line 10).
Let's apply our new found knowledge about FunctionVector:
Let's apply our new found knowledge about FunctionVector:
1 2 3 4 5 6 7 8 9 10 11 | val functionsList = List[Function1[Double,Double]]( Math.sin, Math.sqrt ) val vec = new FunctionVector[Double](functionsList) val output1 = vec.andThen((x: Double) => x*x) val opVec = List[Double=>Double]( (x: Double)=> x+ 1.0, (x: Double)=> Math.exp(-x) ) val output2 = vec.dot(opVec) |
For evaluating our Function vectors classes, we used a list of functions of type Double => Double (line 1): a sinusoidal and a square root functions (line 2).
Once the function vector is created (line 5), it is available to be composed with the existing list of functions of the class FunctionVector (lines 5 and 6).
Once the function vector is created (line 5), it is available to be composed with the existing list of functions of the class FunctionVector (lines 5 and 6).
References
Scala By Example - M. Odersky - June 2014
just information we only provide information for those who need it
ReplyDeleteA. cara agar cepat hamil setelah selesai haid
B. cara agar cepat hamil
C. cara alami untuk segera mendapat kehamilan
D. makanan dan minuman agar cepat hamil
E. masa subur biar cepat hamil
F. panduan agar cepat hamil
Machine Learning Projects for Final Year machine learning projects for final year
DeleteDeep Learning Projects assist final year students with improving your applied Deep Learning skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include Deep Learning projects for final year into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Deep Learning Projects for Final Year even arrange a more significant compensation.
Python Training in Chennai Project Centers in Chennai
Buy all the items in Auckland New Zealand. Treasurebox provide you all the house hold outdoor garden nzitems which will be easily available at low price..!
ReplyDeleteI thank you for the information and articles you provided
ReplyDeleteNew search engine. - 1000 000
ReplyDeletehttps://sr.piecemoney.com/sticaњe/zaraђivaњe-novca-na-mrezhi-istinito-ili-lazhno/
ReplyDeletecool, please guidance so that I can create a blog like yours
ReplyDeletecara melancarkan haid
aktivitas penyebab keguguran
tanda tanda keguguran
manfaat dan bahaya buah nanas
tanda tanda kehamilan
cara membaca hasil usg
hamil muda
cara mengatasi keputihan
pengaruh kista saat hamil
https://no.package-internet.com/cbd/cannabisvital-oil-anmeldelser-meninger-apotek-pris-forum/
ReplyDeletehttps://hr.my-internet.com/smrsaviti/formatic-form-recenzije-misljenja-ljekarna-cijena-forum/
ReplyDeletecena i dawkowanie sliminazer
ReplyDeletethank you for the information provided, we are waiting for the next info
ReplyDeletewatch all Turkish Dramas with English Subtitles free of cost.
ReplyDeleteOn our website you can watch all old and latest turkish dramas with English Subtitles. Visit our website and watch
https://turkishsub.su/
no deposit bonus forex 2021 - takipçi satın al - takipçi satın al - takipçi satın al - tiktok takipçi satın al - instagram beğeni satın al - instagram beğeni satın al - google haritalara yer ekleme - btcturk güvenilir mi - izlenme-satin-al.com - numarasmsonay.com - borsagazete.com - takipcisatinals.com - izlenme-satin-al.com/youtube - google haritalara yer ekleme - altyapısız internet - mikrofiber havlu - forexbonus2020.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word ücretsiz indir - misli apk indir - binance güvenilir mi - takipçi satın al - mikrofiber havlu - uc satın al - takipçi satın al - takipçi satın al - finanspedia.com
ReplyDeleteaşk kitapları
ReplyDeleteyoutube abone satın al
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
tiktok jeton hilesi
tiktok beğeni satın al
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
beğeni satın al
ReplyDeleteinstagram takipçi satın al
ucuz takipçi
takipçi satın al
https://takipcikenti.com
https://ucsatinal.org
instagram takipçi satın al
https://perdemodelleri.org
https://yazanadam.com
instagram takipçi satın al
balon perdeler
petek üstü perde
mutfak tül modelleri
kısa perde modelleri
fon perde modelleri
tül perde modelleri
https://atakanmedya.com
https://fatihmedya.com
https://smmpaketleri.com
https://takipcialdim.com
https://yazanadam.com
yasaklı sitelere giriş
aşk kitapları
yabancı şarkılar
sigorta sorgula
https://cozumlec.com
word indir ücretsiz
tiktok jeton hilesi
rastgele görüntülü sohbet
erkek spor ayakkabı
fitness moves
gym workouts
https://marsbahiscasino.org
http://4mcafee.com
http://paydayloansonlineare.com
Die Technologie entwickelt sich schneller denn je und schneller als Sie denken. Diese aufkommende Technologie wird unsere Lebensweise verändern
ReplyDeleteyeezy
ReplyDeletegolden goose outlet
goyard handbags
jordan shoes
yeezy
kd 12
off white nike
goyard bags
jordan shoes
golden goose outlet