forked from go-nlopt/nlopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlopt_cfunc.go
64 lines (54 loc) · 1.52 KB
/
nlopt_cfunc.go
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
package nlopt
import "C"
import (
"math"
"unsafe"
)
//export nloptFunc
func nloptFunc(n uint, x *C.double, gradient *C.double, fData unsafe.Pointer) C.double {
f := getFunc((*uint8)(fData))
cX := (*[(math.MaxInt32 - 1) / unsafe.Sizeof(*x)]C.double)(unsafe.Pointer(x))[:n:n]
goX := toGoArray(cX)
var goGrad []float64
var cGrad []C.double
if gradient != nil {
cGrad = (*[((math.MaxInt32 - 1) / unsafe.Sizeof(*x))]C.double)(unsafe.Pointer(gradient))[:n:n]
goGrad = toGoArray(cGrad)
}
v := (C.double)(f(goX, goGrad))
if gradient != nil {
for i := 0; i < len(cGrad); i++ {
cGrad[i] = (C.double)((goGrad)[i])
}
}
return v
}
//export nloptMfunc
func nloptMfunc(m uint, result *C.double, n uint, x *C.double, gradient *C.double, fData unsafe.Pointer) {
f := getMfunc((*uint8)(fData))
cX := (*[((math.MaxInt32 - 1) / unsafe.Sizeof(*x))]C.double)(unsafe.Pointer(x))[:n:n]
goX := toGoArray(cX)
var goGrad []float64
var cGrad []C.double
if gradient != nil {
cGrad = (*[((math.MaxInt32 - 1) / unsafe.Sizeof(*x))]C.double)(unsafe.Pointer(gradient))[:n:n]
goGrad = toGoArray(cGrad)
}
var goResult []float64
var cResult []C.double
if result != nil {
cResult = (*[((math.MaxInt32 - 1) / unsafe.Sizeof(*x))]C.double)(unsafe.Pointer(result))[: m*n : m*n]
goResult = toGoArray(cResult)
}
f(goResult, goX, goGrad)
if gradient != nil {
for i := 0; i < len(cGrad); i++ {
cGrad[i] = (C.double)((goGrad)[i])
}
}
if result != nil {
for i := 0; i < len(cResult); i++ {
cResult[i] = (C.double)((goResult)[i])
}
}
}