Exemplo n.º 1
0
func Shuffle(rng *rng.GslRng, data interface{}, n int) {
	// data must be a slice
	dataType := reflect.TypeOf(data)
	if dataType.Kind() != reflect.Slice {
		gogsl.Error("rng.Shuffle() must have a slice as its second argument", gogsl.GSL_EINVAL)
		return
	}
	baseType := dataType.Elem()
	dataVal := reflect.ValueOf(data)
	sliceN := dataVal.Len()
	if sliceN < 0 {
		gogsl.Error("shuffle length may not be negative in rng.Shuffle()", gogsl.GSL_EINVAL)
		return
	}
	if sliceN < n {
		gogsl.Error("shuffle length too large for slice in rng.Shuffle()", gogsl.GSL_EINVAL)
		return
	}
	elementLen := baseType.Size()
	addr := dataVal.Index(0).UnsafeAddr()

	//hdr := (*reflect.SliceHeader)(unsafe.Pointer(addr))
	//void gsl_ran_shuffle (const gsl_rng * r, void * base, size_t n, size_t size)
	C.gsl_ran_shuffle((*C.gsl_rng)(unsafe.Pointer(rng.Ptr())),
		unsafe.Pointer(addr),
		C.size_t(n), C.size_t(elementLen))
}
Exemplo n.º 2
0
func (h *GslHistogram) Slice_(ofs, length int) []float64 {
	if length+ofs > h.Size() {
		gogsl.Error("histogram slice out of range", gogsl.GSL_ERANGE)
		return nil
	}
	ptr := C.histogram_get_value_ptr((*C.gsl_histogram)(unsafe.Pointer(h.Ptr())), C.size_t(ofs))
	srcHdr := &reflect.SliceHeader{Data: uintptr(ptr), Len: length, Cap: length}
	src := *(*[]float64)(unsafe.Pointer(srcHdr))
	return src
}
Exemplo n.º 3
0
func (h *GslHistogram2d) RangeSliceY_(yofs, ylength int) []float64 {
	dim := h.Dim()
	if ylength+yofs > dim[0] {
		gogsl.Error("histogram slice out of range", gogsl.GSL_ERANGE)
		return nil
	}
	ptr := C.histogram2d_get_range_ptr_y((*C.gsl_histogram2d)(unsafe.Pointer(h.Ptr())), C.size_t(yofs))
	srcHdr := &reflect.SliceHeader{Data: uintptr(ptr), Len: ylength, Cap: ylength}
	src := *(*[]float64)(unsafe.Pointer(srcHdr))
	return src
}
Exemplo n.º 4
0
//gsl_ntuple * gsl_ntuple_create (char * filename, void * ntuple_data, size_t size)
func CreateOrOpen(filename string, data interface{}, op OpenOperation) *GslNtuple {

	// Check the data type and make sure it is a slice of some concrete type.
	dataType := reflect.TypeOf(data)
	if dataType.Kind() != reflect.Slice {
		gogsl.Error("ntuple.Create() data argument must be a slice", gogsl.GSL_EINVAL)
		return nil
	}
	dataValue := reflect.ValueOf(data)
	baseType := dataType.Elem()
	dataSize := int(baseType.Size()) * dataValue.Len()
	if !dataValue.CanAddr() {
		x := reflect.New(dataValue.Type()).Elem()
		x.Set(dataValue)
		dataValue = x
	}
	srcHdr := (*reflect.SliceHeader)(unsafe.Pointer(dataValue.UnsafeAddr()))
	cFile := C.CString(filename)
	var tuplePtr *C.gsl_ntuple
	switch op {
	case OPEN:
		{
			tuplePtr = C.gsl_ntuple_open(cFile, unsafe.Pointer(srcHdr.Data), C.size_t(dataSize))
		}
	case CREATE:
		{
			tuplePtr = C.gsl_ntuple_create(cFile, unsafe.Pointer(srcHdr.Data), C.size_t(dataSize))
		}
	default:
		{
			gogsl.Error("unknown open operation", gogsl.GSL_EINVAL)
			return nil
		}
	}
	result := &GslNtuple{baseType: baseType, refLen: dataValue.Len(), elementSize: int(baseType.Size()), ref: data}
	gogsl.InitializeGslReference(result, uintptr(unsafe.Pointer(tuplePtr)))
	C.free(unsafe.Pointer(cFile))
	return result
}
Exemplo n.º 5
0
func (h *GslHistogram2d) RangeSliceX(xofs, xlength int) []float64 {
	dim := h.Dim()
	if xlength+xofs > dim[0] {
		gogsl.Error("histogram slice out of range", gogsl.GSL_ERANGE)
		return nil
	}
	ptr := C.histogram2d_get_range_ptr_x((*C.gsl_histogram2d)(unsafe.Pointer(h.Ptr())), C.size_t(xofs))
	srcHdr := &reflect.SliceHeader{Data: uintptr(ptr), Len: xlength, Cap: xlength}
	src := *(*[]float64)(unsafe.Pointer(srcHdr))
	dst := make([]float64, xlength)
	copy(dst, src)
	return dst
}
Exemplo n.º 6
0
// void gsl_ran_sample (const gsl_rng * r, void * dest, size_t k, void * src, size_t n, size_t size)
func Sample(rng *rng.GslRng, dest interface{}, src interface{}, n int) {
	// data must be a slice
	destDataType := reflect.TypeOf(dest)
	if destDataType.Kind() != reflect.Slice {
		gogsl.Error("rng.Sample() must have a slice as its second argument", gogsl.GSL_EINVAL)
		return
	}
	srcDataType := reflect.TypeOf(src)
	if srcDataType.Kind() != reflect.Slice {
		gogsl.Error("rng.Sample() must have a slice as its third argument", gogsl.GSL_EINVAL)
		return
	}
	if destDataType != srcDataType {
		gogsl.Error("rng.Sample() source and destination data types must match", gogsl.GSL_EINVAL)
		return
	}
	if n < 0 {
		gogsl.Error("length may not be negative in rng.Sample()", gogsl.GSL_EINVAL)
		return
	}
	baseType := destDataType.Elem()
	destVal := reflect.ValueOf(dest)
	srcVal := reflect.ValueOf(src)
	sliceN := destVal.Len()
	if sliceN < n {
		gogsl.Error("sample length too large for slice in rng.Sample()", gogsl.GSL_EINVAL)
		return
	}
	elementLen := baseType.Size()
	srcAddr := srcVal.Index(0).UnsafeAddr()
	destAddr := destVal.Index(0).UnsafeAddr()
	C.gsl_ran_sample((*C.gsl_rng)(unsafe.Pointer(rng.Ptr())),
		unsafe.Pointer(destAddr), C.size_t(sliceN),
		unsafe.Pointer(srcAddr), C.size_t(srcVal.Len()),
		C.size_t(elementLen))
}
Exemplo n.º 7
0
func (h *GslHistogram2d) Slice_(xofs, xlength, yofs, ylength int) *matrix.GslMatrixView {
	dim := h.Dim()
	if xlength+xofs > dim[0] ||
		ylength+yofs > dim[1] {
		gogsl.Error("histogram slice out of range", gogsl.GSL_ERANGE)
		return nil
	}
	// bin[i * ny + j]
	ptrOfs := xofs*dim[1] + yofs
	ptr := C.histogram2d_get_value_ptr((*C.gsl_histogram2d)(unsafe.Pointer(h.Ptr())), C.size_t(ptrOfs))
	dimlen := dim[0]*dim[1] - ptrOfs
	srcHdr := &reflect.SliceHeader{Data: uintptr(ptr), Len: dimlen, Cap: dimlen}
	src := *(*[]float64)(unsafe.Pointer(srcHdr))
	view := matrix.ViewArrayWithTda(src, xlength, ylength, dim[1])
	return view
}