我准备一个随机数矩阵,计算它的逆矩阵和矩阵乘以原始矩阵.理论上,这给出了单位矩阵.我numpy该怎么办呢?
import numpy
A = numpy.zeros((100,100))
E = numpy.zeros((100,100))
size = 100
for i in range(size):
for j in range(size):
A[i][j]+=numpy.random.randint(10)
if i == j:
E[i][j]+=1
A_inv = numpy.linalg.linalg.inv(A)
print numpy.dot(A, A_inv)
Run Code Online (Sandbox Code Playgroud)
运行代码会产生
[me]machine @ numeric $ python rand_diag.py
[[ 1.00000000e+00 -7.99360578e-15 -1.14491749e-16 ..., 3.81639165e-17
-4.42701431e-15 1.17961196e-15]
[ -5.55111512e-16 1.00000000e+00 -2.22044605e-16 ..., -3.88578059e-16
1.33226763e-15 -8.32667268e-16]
Run Code Online (Sandbox Code Playgroud)
很明显,结果是一个单位矩阵,但不是很精确,print numpy.dot(A, A_inv) == E显然给出了False.我这样做是为了练习线性代数,并试图找到我的机器到达其极限的矩阵的大小.得到一个True教学会很有吸引力.
编辑:
设置size=10000,我的内存不足
[me]machine @ numeric $ Python(794) malloc:
***mmap(size=800002048) failed …Run Code Online (Sandbox Code Playgroud) 我有第一个时间序列,分辨率为每分钟 1 个数据点:
11.02.16 13:29:00 247.68
11.02.16 13:30:00 398.14
11.02.16 13:31:00 442.12
11.02.16 13:32:00 452.53
11.02.16 13:33:00 449.06
11.02.16 13:34:00 453.69
11.02.16 13:35:00 458.32
11.02.16 13:36:00 453.69
11.02.16 13:37:00 453.69
11.02.16 13:38:00 454.85
11.02.16 13:39:00 454.85
[...] continued
Run Code Online (Sandbox Code Playgroud)
可以使用 xy 图轻松绘制。
现在是第二个时间序列,数据间隔不太规则(但规模相同并覆盖第一个时间序列):
11.02.2016 18:04:59 102098.00
12.02.2016 05:51:52 110932.50
12.02.2016 13:31:21 100924.00
12.02.2016 23:37:22 101369.50
12.02.2016 23:38:22 94225.50
13.02.2016 08:36:09 99236.00
13.02.2016 08:36:53 93832.50
13.02.2016 17:20:12 99556.50
Run Code Online (Sandbox Code Playgroud)
如何将第二个系列叠加在第一个系列上?当然,y 值将位于主轴和次轴上,但 x 轴(时间)是相同的维度。
我试图找到两个润滑日期向量的交集。但是,在应用该lubridate::intersect函数之后,日期将转换为双精度值。
library(lubridate)
> a <- c(ymd("2017-01-01"), ymd("2017-01-02"), ymd("2017-01-03"), ymd("2017-01-04"))
> b <- c(ymd("2017-01-02"), ymd("2017-01-03"), ymd("2017-01-04"), ymd("2017-01-05"))
> lubridate::intersect(a, b)
[1] 17168 17169 17170
Run Code Online (Sandbox Code Playgroud)
如何保留原始日期类型?
我有一个removeIf {}在Groovy中处理的集合。在块内,我可以访问一些it标识符。这是什么,在哪里记录?
给定这个函数的定义f:
f :: [Int] -> [Int]
f [] = []
f (x:xs) = x:[]
Run Code Online (Sandbox Code Playgroud)
我假设像
f [1]
Run Code Online (Sandbox Code Playgroud)
不会匹配,因为模式(x:xs)仅匹配,如果列表中的xs后面有更多元素x,则列表不是这种情况[1]。还是?
我正在使用^category/|categories/$.
为什么不起作用^categor[y|ies]/$?
我想使用R下载一个google docs电子表格,然后将电子表格作为csv文件导入一个小巧的服务器应用程序中.我怎么能这样做?
我正在尝试编写一个简单的复制程序。它读取test_data.txt100 个字节的块并将这些字节复制到test_dest.txt. 我发现目标文件至少chunk比源文件大一个单位。我如何调整它以便只复制正确数量的字节?我需要一个大小为 1 的复制缓冲区吗?请不要使用低级 I/O 系统调用来解决它。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int main() {
int fh = open("test_data.txt", O_RDONLY);
int trg = open("test_dest.txt", O_CREAT | O_WRONLY);
int BUF_SIZE = 100;
char inp[BUF_SIZE];
int read_bytes = read(fh, inp, BUF_SIZE);
while (read_bytes > 0) {
write(trg, inp, BUF_SIZE);
read_bytes = read(fh, inp, BUF_SIZE);
}
close(trg);
close(fh);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 模板函数作为一般和特殊情况存在:
#include <string>
#include <iostream>
using namespace std;
// General case
template<typename X> void whichOneIsLarger(X a, X b) {
(a > b) ? cout << "x [" << a << "] is larger than y [" << b << "]" : cout << "y [" << b << "] is larger than x [" << a << "]" << endl;
}
// Specialization
template<> void whichOneIsLarger<string>(string a, string b) {
(a.size() > b.size()) ? cout << "x [" << a …Run Code Online (Sandbox Code Playgroud) 下面,我将标题固定在顶部,设置width: 100%并应用了body元素的边距margin: 10%.我希望标题保持在视口的100%宽度,但它现在有一个左边距但到达右侧视口的末尾.
* {
box-sizing: border-box;
}
header {
position: fixed;
background-color: #59b1ff;
top: 0;
height: 20px;
width: 100%;
}
body {
margin: 20%;
}
div {
border: 1px solid grey;
}Run Code Online (Sandbox Code Playgroud)
<header>Header</header>
<main>
<div>Div</div>
</main>Run Code Online (Sandbox Code Playgroud)
为什么标题没有附加到左视口边缘?
想象一个 Haskell 类型
data Person = P String
Run Code Online (Sandbox Code Playgroud)
实例化一个带括号或不带括号的人有区别吗?我真的没有看到任何:
*Main> :t P "Tim"
P "Tim" :: Person
*Main> :t (P "Tim")
(P "Tim") :: Person
*Main> p1 = P "Tim"
*Main> p2 = (P "Tim")
*Main> p1
P "Tim"
*Main> p2
P "Tim"
Run Code Online (Sandbox Code Playgroud) 我想在 C 程序中启动两个子进程。我使用了fork()两次,但是在检查pids 时,它看起来好像启动了四个进程:
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
int main(int argc, char *argv[]) {
pid_t one = fork();
pid_t two = fork();
if (one == 0) {
printf("child pid = %d\n", getpid());
}
if (two == 0) {
printf("child pid = %d\n", getpid());
}
if (one != 0 && two != 0) {
wait(NULL);
printf("parent pid = %d\n", getpid());
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
g@x:dir$ ./twochilds
child pid = 11408
child pid = 11407 …Run Code Online (Sandbox Code Playgroud)