抽象出 Matrix 库依赖项 [英] Abstracting away Matrix library dependency

查看:21
本文介绍了抽象出 Matrix 库依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个玩具线性代数库,用于学习和用于玩具神经网络库.我想使用不同的 Java 线性代数库来测试效率,但我被困在抽象中.

I'm writing a toy linear algebra library for learning and to be used in a toy neural network library. I would like to use different Java linear algebra libraries to test efficiency, but I'm stuck at the abstraction.

假设我希望我自己的矩阵抽象像这样:

Suppose I would like my own Matrix abstraction to be add, subtract, multiply, hadamardMultiply, map, and mapElements like so:

// I would prefer for this to force the implementing classes to be immutable...
// M is the type of implementing matrix
public interface Matrix<M,T extends Number> {
    M add(M in); // return this matrix + in
    // overload add(T)
    // default subtract = add (-1 * T)
    M multiply(M in); // return this matrix X in
    // overload multiply(T)
    // default divide = multiply(T^-1)
    M hadamardMultiply(M in); // return this matrix hadamard in
    T map(Function<M,T> map); // f: M -> T
    M mapElements(UnaryOperator<T> map); // f: T -> T
}

我所说的不可变是指我的 API 应该看起来像

What I mean by immutable is that my API should look like

Matrix<vendor.matrix.VendorMatrix, Double> multiple = myMatrix.multiply(someOtherMatrix);
// or
Matrix<vendor.matrix.VendorMatrix, Double> add = myMatrix.add(5);
// and
Matrix<vendor.matrix.VendorMatrix, Double> mapped = myMatrix.map(e -> e / Math.PI);

不应改变 myMatrix.

which should not alter the myMatrix.

现在我以前使用过 UJMP,所以我需要围绕该库实现这个包装器,在这里是我偶然发现这些方法无法返回我的矩阵的问题,它们必须在实现类中返回矩阵的类型.然而,这打破了抽象.

Now I've previously used UJMP, and so I need to implement this wrapper around that library, and here is where I stumbled upon the issue that these methods cannot return my Matrix, they have to return the type of the matrices in implementing classes. However, this breaks the abstraction.

所以我认为下一步是创建一个 UJMPMatrix 类,它扩展任何库(在本例中为 UJMP)所需的矩阵类并像这样实现我的 Matrix 接口:

So I thought that the next step would be to make a UJMPMatrix class, which extends whichever library's (in this case UJMP) desired matrix class and implements my Matrix interface like so:

public class UJMPMatrix extends org.ujmp.core.DefaultDenseDoubleMatrix2D 
    implements my.library.Matrix<org.ujmp.core.Matrix, Double> {
....
}

有了这个,我现在已经失去了抽象,因为 defaultdensedoublematrix2d 已经包含所有这些方法,而我只想要在我的界面中提供的方法.我该如何继续?

With this, I have now lost the abstraction, as the defaultdensedoublematrix2d already has all of these methods in them, when I only want the ones provided in my interface. How do I proceed?

推荐答案

如果 UJMPMatrix 扩展了 DefaultDenseDoubleMatrix2D,那么所有公共 UJMP 方法都会公开.您需要使用委托模式.

If UJMPMatrix extends DefaultDenseDoubleMatrix2D then all the public UJMP methods are exposed. You need to use the delegate pattern.

关于泛型类型,您还需要更改它,以便它可以接受其他 UJMPMatrix 作为方法参数.

Regarding the generics typing you will also need change it so it can accept other UJMPMatrix as method arguments.

public class UJMPMatrix implements my.library.Matrix<UJMPMatrix, Double> {
    private org.ujmp.core.DefaultDenseDoubleMatrix2D delegate;
    
    // Implement the methods of your interface by using the delegate
}

这篇关于抽象出 Matrix 库依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆