- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章使用java写的矩阵乘法实例(Strassen算法)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
Strassen算法于1969年由德国数学家Strassen提出,该方法引入七个中间变量,每个中间变量都只需要进行一次乘法运算。而朴素算法却需要进行8次乘法运算.
。
Strassen算法的原理如下所示,使用sympy验证Strassen算法的正确性 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import
sympy as s
A = s.Symbol(
"A"
)
B = s.Symbol(
"B"
)
C = s.Symbol(
"C"
)
D = s.Symbol(
"D"
)
E = s.Symbol(
"E"
)
F = s.Symbol(
"F"
)
G = s.Symbol(
"G"
)
H = s.Symbol(
"H"
)
p1 = A * (F - H)
p2 = (A + B) * H
p3 = (C + D) * E
p4 = D * (G - E)
p5 = (A + D) * (E + H)
p6 = (B - D) * (G + H)
p7 = (A - C) * (E + F)
print(A * E + B * G, (p5 + p4 - p2 + p6).simplify())
print(A * F + B * H, (p1 + p2).simplify())
print(C * E + D * G, (p3 + p4).simplify())
print(C * F + D * H, (p1 + p5 - p3 - p7).simplify())
|
$$f(N)=7\times f(\frac{N}{2})=7^2\times f(\frac{N}{4})=...=7^k\times f(\frac{N}{2^k})$$ 。
最终复杂度为$7^{log_2 N}=N^{log_2 7}$ 。
。
代码如下,可以看看数据结构的定义,时间换空间.
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
public
class
Matrix {
private
final
Matrix[] _matrixArray;
private
final
int
n;
private
int
element;
public
Matrix(
int
n) {
this
.n = n;
if
(n !=
1
) {
this
._matrixArray =
new
Matrix[
4
];
for
(
int
i =
0
; i <
4
; i++) {
this
._matrixArray[i] =
new
Matrix(n /
2
);
}
}
else
{
this
._matrixArray =
null
;
}
}
private
Matrix(
int
n,
boolean
needInit) {
this
.n = n;
if
(n !=
1
) {
this
._matrixArray =
new
Matrix[
4
];
}
else
{
this
._matrixArray =
null
;
}
}
public
void
set(
int
i,
int
j,
int
a) {
if
(n ==
1
) {
element = a;
}
else
{
int
size = n /
2
;
this
._matrixArray[(i / size) *
2
+ (j / size)].set(i % size, j % size, a);
}
}
public
Matrix multi(Matrix m) {
Matrix result =
null
;
if
(n ==
1
) {
result =
new
Matrix(
1
);
result.set(
0
,
0
, (element * m.element));
}
else
{
result =
new
Matrix(n,
false
);
result._matrixArray[
0
] = P5(m).add(P4(m)).minus(P2(m)).add(P6(m));
result._matrixArray[
1
] = P1(m).add(P2(m));
result._matrixArray[
2
] = P3(m).add(P4(m));
result._matrixArray[
3
] = P5(m).add(P1(m)).minus(P3(m)).minus(P7(m));
}
return
result;
}
public
Matrix add(Matrix m) {
Matrix result =
null
;
if
(n ==
1
) {
result =
new
Matrix(
1
);
result.set(
0
,
0
, (element + m.element));
}
else
{
result =
new
Matrix(n,
false
);
result._matrixArray[
0
] =
this
._matrixArray[
0
].add(m._matrixArray[
0
]);
result._matrixArray[
1
] =
this
._matrixArray[
1
].add(m._matrixArray[
1
]);
result._matrixArray[
2
] =
this
._matrixArray[
2
].add(m._matrixArray[
2
]);
result._matrixArray[
3
] =
this
._matrixArray[
3
].add(m._matrixArray[
3
]);;
}
return
result;
}
public
Matrix minus(Matrix m) {
Matrix result =
null
;
if
(n ==
1
) {
result =
new
Matrix(
1
);
result.set(
0
,
0
, (element - m.element));
}
else
{
result =
new
Matrix(n,
false
);
result._matrixArray[
0
] =
this
._matrixArray[
0
].minus(m._matrixArray[
0
]);
result._matrixArray[
1
] =
this
._matrixArray[
1
].minus(m._matrixArray[
1
]);
result._matrixArray[
2
] =
this
._matrixArray[
2
].minus(m._matrixArray[
2
]);
result._matrixArray[
3
] =
this
._matrixArray[
3
].minus(m._matrixArray[
3
]);;
}
return
result;
}
protected
Matrix P1(Matrix m) {
return
_matrixArray[
0
].multi(m._matrixArray[
1
]).minus(_matrixArray[
0
].multi(m._matrixArray[
3
]));
}
protected
Matrix P2(Matrix m) {
return
_matrixArray[
0
].multi(m._matrixArray[
3
]).add(_matrixArray[
1
].multi(m._matrixArray[
3
]));
}
protected
Matrix P3(Matrix m) {
return
_matrixArray[
2
].multi(m._matrixArray[
0
]).add(_matrixArray[
3
].multi(m._matrixArray[
0
]));
}
protected
Matrix P4(Matrix m) {
return
_matrixArray[
3
].multi(m._matrixArray[
2
]).minus(_matrixArray[
3
].multi(m._matrixArray[
0
]));
}
protected
Matrix P5(Matrix m) {
return
(_matrixArray[
0
].add(_matrixArray[
3
])).multi(m._matrixArray[
0
].add(m._matrixArray[
3
]));
}
protected
Matrix P6(Matrix m) {
return
(_matrixArray[
1
].minus(_matrixArray[
3
])).multi(m._matrixArray[
2
].add(m._matrixArray[
3
]));
}
protected
Matrix P7(Matrix m) {
return
(_matrixArray[
0
].minus(_matrixArray[
2
])).multi(m._matrixArray[
0
].add(m._matrixArray[
1
]));
}
public
int
get(
int
i,
int
j) {
if
(n ==
1
) {
return
element;
}
else
{
int
size = n /
2
;
return
this
._matrixArray[(i / size) *
2
+ (j / size)].get(i % size, j % size);
}
}
public
void
display() {
for
(
int
i =
0
; i < n; i++) {
for
(
int
j =
0
; j < n; j++) {
System.out.print(get(i, j));
System.out.print(
" "
);
}
System.out.println();
}
}
public
static
void
main(String[] args) {
Matrix m =
new
Matrix(
2
);
Matrix n =
new
Matrix(
2
);
m.set(
0
,
0
,
1
);
m.set(
0
,
1
,
3
);
m.set(
1
,
0
,
5
);
m.set(
1
,
1
,
7
);
n.set(
0
,
0
,
8
);
n.set(
0
,
1
,
4
);
n.set(
1
,
0
,
6
);
n.set(
1
,
1
,
2
);
Matrix res = m.multi(n);
res.display();
}
}
|
。
到此这篇关于使用java写的矩阵乘法的文章就介绍到这了,更多相关java矩阵乘法(Strassen算法)内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/wj310298/article/details/44857175 。
最后此篇关于使用java写的矩阵乘法实例(Strassen算法)的文章就讲到这里了,如果你想了解更多关于使用java写的矩阵乘法实例(Strassen算法)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我需要(我必须)将大量 float 写入 qdatastream 并且我只使用 4 个字节是必要的。setFloatingPointPrecision 或为 float 和 double 写入 4 或
我有一些 C 代码,我用 Python 对其进行了扩展。扩展的 C 代码有一个将一些结构附加到二进制文件的函数: void writefunction(const struct struct1* so
我正在用 C 语言开发一个小软件,用于在布告栏中读取和写入消息。每条消息都是一个以渐进数字命名的 .txt。 软件是多线程的,有很多用户可以并发操作。 用户可以进行的操作有: 阅读整个公告板(所有 .
我有 2 个线程同时访问同一个大文件 (.txt)。 第一个线程正在从文件中读取。第二个线程正在写入文件。 两个线程都访问同一个 block ,例如(开始:0, block 大小:10),但具有不同的
我做了很多谷歌搜索,但我仍然不确定如何继续。 Linux 下最常见的剪贴板读写方式是什么?我想要同时支持 Gnome 和 KDE 桌面。 更新:我是否认为没有简单的解决方案,必须将多个来源(gnome
1. 定义配置文件信息 有时候我们为了统一管理会把一些变量放到 yml 配置文件中 例如 图片 用 @ConfigurationProperties 代替 @Value 使用方法 定义对应字段的实体
在开始之前,我必须先声明我是 FORTRAN 的新手。我正在维护 1978 年的一段遗留代码。它的目的是从文件中读取一些数据值,处理这些值,然后将处理过的值输出到另一个文本文件。 给定以下 FORTR
我正在制作一个应用程序,我需要存储用户提供的一些信息。我尝试使用 .plist 文件来存储信息,我发现: NSString *filePath = @"/Users/Denis/Documents/X
在delphi类中声明属性时是否可能有不同类型的结果? 示例: 属性月份:字符串读取monthGet(字符串)写入monthSet(整数); 在示例中,我希望在属性(property)月份中,当我:读
我正在以二进制形式将文件加载到数组中,这似乎需要一段时间有没有更好更快更有效的方法来做到这一点。我正在使用类似的方法写回文件。 procedure openfile(fname:string); va
我想实现一个运行模拟的C#控制台应用程序。另外,我想给用户机会在控制台上按“+”或“-”来加速/减速模拟的速度。 有没有办法在编写控制台时读取控制台?我相信我可以为此使用多线程,但是我却不怎么做(我对
这是我的代码: use std::fs::File; use std::io::Write; fn main() { let f = File::create("").unwrap();
我有一个应用程序可以访问 csv 文本文件中的单词。由于它们通常不会更改,因此我将它们放置在 .jar 文件中,并使用 .getResourceAsStream 调用读取它们。我真的很喜欢这种方法,因
我使用kubeadm,docker 17.12.1-ce和法兰绒网络安装了Kubernetes 1.13.1集群 但是,我发现Kubernetes主服务器上有许多空文件,权限为666,该文件允许任何用
我的工作区中有一些 java 文件。现在我想编写一个java程序,它可以读取来自不同源的文本文件,一次一个,一行一行,并将这些行插入到工作区中各自的java文件中。 文本文件会告诉我将哪个文件插入到哪
用户A要求系统读取文件foo,同时用户B想要将他或她的数据保存到同一个文件中。在文件系统级别如何处理这种情况? 最佳答案 大多数文件系统(但不是全部)使用锁定来保护对同一文件的并发访问。锁可以是独占的
我对保护移动应用程序的 firebase 数据库有一些疑问。 例如,在反编译Android应用程序后,黑客可以获取firebase api key 然后访问firebase数据库,这是正确的吗? 假设
我想让文件从外部不可删除,并希望使用java从程序对该文件进行读/写操作。 S0,我使用以下代码使用java创建了不可删除的文件: Process pcs = Runtime.getRunti
当 Selector.select() 以阻塞模式等待读/写操作时,是否可以将写消息推送到客户端?如何将选择器从阻塞模式移至写入模式?触发器可以是一个后台线程,用于放置需要写入给定 channel 的
我目前正在学习在 Linux 环境中使用 C 进行套接字编程。作为一个项目,我正在尝试编写一个基本的聊天服务器和客户端。 目的是让服务器为每个连接的客户端派生一个进程。 我遇到的问题是读取一个 chi
我是一名优秀的程序员,十分优秀!