예제 #1
0
파일: solve.go 프로젝트: postfix/sparse-1
func main() {
	flag.Parse()
	if flag.NArg() == 0 {
		log.Fatal("missing file name")
	}
	name := flag.Args()[0]

	f, err := os.Open(name)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	var r io.Reader
	if path.Ext(name) == ".gz" {
		gz, err := gzip.NewReader(f)
		if err != nil {
			log.Fatal(err)
		}
		name = strings.TrimSuffix(name, ".gz")
		r = gz
	} else {
		r = f
	}

	var aDok *sparse.DOK
	switch path.Ext(name) {
	case ".mtx":
		aDok, err = readMatrixMarket(r)
	case ".rsa":
		log.Fatal("reading of Harwell-Boeing format not yet implemented")
	default:
		log.Fatal("unknown file extension")
	}
	if err != nil {
		log.Fatal(err)
	}

	a := sparse.NewCSR(aDok)
	n, _ := a.Dims()
	x := make([]float64, n)
	for i := range x {
		x[i] = 1
	}
	b := make([]float64, n)
	sparse.MulMatVec(1, false, a, x, 1, 0, b, 1)
	for i := range x {
		x[i] = 0
	}
	result, err := iterative.Solve(a, b, x, nil, &iterative.CG{})
	if err != nil {
		log.Fatal(err)
	}
	if len(result.X) > 10 {
		fmt.Println("Solution[:10]:", result.X[:10])
	} else {
		fmt.Println("Solution:", result.X)
	}
}
예제 #2
0
파일: solve.go 프로젝트: vladimir-ch/sparse
func main() {
	flag.Parse()
	if flag.NArg() == 0 {
		log.Fatal("missing file name")
	}
	name := flag.Args()[0]

	f, err := os.Open(name)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	var r io.Reader
	if path.Ext(name) == ".gz" {
		gz, err := gzip.NewReader(f)
		if err != nil {
			log.Fatal(err)
		}
		name = strings.TrimSuffix(name, ".gz")
		r = gz
	} else {
		r = f
	}

	// blas64.Use(cgo.Implementation{})
	blas64.Use(native.Implementation{})

	var aDok *sparse.DOK
	switch path.Ext(name) {
	case ".mtx":
		aDok, err = readMatrixMarket(r)
	case ".rsa":
		log.Fatal("reading of Harwell-Boeing format not yet implemented")
	default:
		log.Fatal("unknown file extension")
	}
	if err != nil {
		log.Fatal(err)
	}

	a := sparse.NewCSR(aDok)
	n, _ := a.Dims()

	// Create the right-hand side so that the solution is [1 1 ... 1].
	x := make([]float64, n)
	for i := range x {
		x[i] = 1
	}
	xVec := mat64.NewVector(n, x)
	bVec := mat64.NewVector(n, make([]float64, n))
	sparse.MulMatVec(bVec, 1, false, a, xVec)

	result, err := iterative.Solve(a, bVec, nil, nil, &iterative.CG{})
	if err != nil {
		log.Fatal(err)
	}
	if result.X.Len() > 10 {
		fmt.Println("Solution[:10]:", result.X.RawVector().Data[:10])
	} else {
		fmt.Println("Solution:", result.X.RawVector())
	}
}