我已经构建了一个小代码,我想用它来解决涉及大型稀疏矩阵的特征值问题.它运行正常,我现在要做的就是将稀疏矩阵中的一些元素设置为零,即最顶行中的元素(对应于实现边界条件).我可以调整下面的列向量(C0,C1和C2)来实现这一点.但是,我想知道是否有更直接的方式.显然,NumPy索引不适用于SciPy的稀疏包.
import scipy.sparse as sp
import scipy.sparse.linalg as la
import numpy as np
import matplotlib.pyplot as plt
#discretize x-axis
N = 11
x = np.linspace(-5,5,N)
print(x)
V = x * x / 2
h = len(x)/(N)
hi2 = 1./(h**2)
#discretize Schroedinger Equation, i.e. build
#banded matrix from difference equation
C0 = np.ones(N)*30. + V
C1 = np.ones(N) * -16.
C2 = np.ones(N) * 1.
diagonals = np.array([-2,-1,0,1,2])
H = sp.spdiags([C2, C1, C0,C1,C2],[-2,-1,0,1,2], N, N)
H *= hi2 * (- 1./12.) …Run Code Online (Sandbox Code Playgroud) 我是Fortran的血腥初学者.
我想知道:Fortran文件的.f和.F扩展名之间有什么区别.我知道的很多:f是免费的,F是固定的.
免费和固定实际意味着什么,为什么它们完全搞砸了编译过程?(我正在使用gfortran和ifort).
我用Google搜索了f和F扩展名之间的差异.我对结果不满意,这就是我转向stackoverflow的原因.
如果你能提出一个很好的参考资料来阅读,我会非常乐意这样做.
我正在尝试在D3 sankey中订购节点.我希望将值较大的节点绘制得更高.
这是相关的代码(基于d3 noob的代码)片段,我试图通过使用D3的内置排序功能来实现我想要的.结果不是我所期望的.
在检查sankey.js时,我并不完全清楚插件是如何命令节点的.任何有关该主题的建议将不胜感激.
//set up graph in same style as original example but empty
graph = {"nodes" : [], "links" : []};
data.forEach(function (d) {
graph.nodes.push({ "name": d.source });
graph.nodes.push({ "name": d.target });
graph.links.push({ "source": d.source,
"target": d.target,
"value": +d.value });
});
//try to sort the nodes and links before indexing them (not working)
graph.nodes.sort(function (a,b) {return d3.descending(a.value, b.value); });
graph.links.sort(function (a,b) {return d3.descending(a.value, b.value); });
// return only the distinct / unique nodes
graph.nodes = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用openMP优化下面函数generate_histogram()中的嵌套for循环.基于我在SE帖子中读到的内容,我尝试了不同的编译指示组合.
问题是嵌套for循环在没有openMP的情况下比使用openMP更快!
如果我尝试使用简化而不是原子编译来并行化我的代码,我最终会遇到netchunk失败.有人知道这个花哨的调整吗?我试图将数据分成直方图.因此,直方图在实际代码中的大小可变,与下面的代码段不同.
#include<stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define float_t float
#include <time.h>
#include <omp.h>
float_t generate_histogram(float_t **matrix, int *histogram, int mat_size, int hist_size)
{
int i,j,k,count;
float_t max = 0.;
float_t sum;
//set histogram to zero everywhere
for(i = 0; i < hist_size; i++)
histogram[i] = 0;
//matrix computations
#pragma omp parallel for private(i) shared(histogram,j,k,max) schedule(dynamic)
//#pragma omp parallel for schedule(runtime)
for (i = 1; i < (mat_size-1); i++)
{
#pragma omp parallel …Run Code Online (Sandbox Code Playgroud) 我刚刚遇到这个Python代码,我的问题是关于print语句中的语法:
class Point(object):
"""blub"""
#class variables and methods
blank = Point
blank.x = 3.0
blank.y = 4.0
print('(%g,%g)' % (blank.x,blank.y))
Run Code Online (Sandbox Code Playgroud)
此print语句只打印( 3.0,4.0 ),即blank.x和blank.y中的值.
我不理解最后一行(blank.x,blank.y)前面的%运算符.它做了什么以及在哪里可以在文档中找到它?
谷歌搜索这个,我总是以模数运算符结束.
我通常在python中编程.为了提高我的模拟性能,我正在学习C.在向链表实现追加函数时,我有一个问题需要理解指针指针的使用.这是我的书(由Kanetkar理解C指针)中的代码的摘录.
#include <stdlib.h>
#include <stdio.h>
struct node{
int data;
struct node *link;
};
int main(){
struct node *p; //pointer to node structure
p = NULL; //linked list is empty
append( &p,1);
return 0;
}
append( struct node **q, int num){
struct node *temp, *r; //two pointers to struct node
temp = *q;
if(*q == NULL){
temp = malloc(sizeof(struct node));
temp -> data = num;
temp -> link = NULL;
*q = temp;
}
else{
temp = *q;
while( temp -> …Run Code Online (Sandbox Code Playgroud) 编码一些量子力学例程,我发现了Python的NumPy的奇怪行为.当我使用NumPy乘以两个以上的数组时,我会得到错误的结果.在下面的代码中,我必须写:
f = np.multiply(rowH,colH)
A[row][col]=np.sum(np.multiply(f,w))
Run Code Online (Sandbox Code Playgroud)
这会产生正确的结果.但是,我最初的表述是这样的:
A[row][col]=np.sum(np.multiply(rowH, colH, w))
Run Code Online (Sandbox Code Playgroud)
它不会产生错误信息,但结果错误.我认为我可以将三个阵列赋予numpy的多重例程,我的错在哪里?
这是完整的代码:
from numpy.polynomial.hermite import Hermite, hermgauss
import numpy as np
import matplotlib.pyplot as plt
dim = 3
x,w = hermgauss(dim)
A = np.zeros((dim, dim))
#build matrix
for row in range(0, dim):
rowH = Hermite.basis(row)(x)
for col in range(0, dim):
colH = Hermite.basis(col)(x)
#gaussian quadrature in vectorized form
f = np.multiply(rowH,colH)
A[row][col]=np.sum(np.multiply(f,w))
print(A)
Run Code Online (Sandbox Code Playgroud)
:: NOTE ::此代码仅与NumPy 1.7.0及更高版本一起运行!
我正在尝试在C中优化我的一些代码,这比下面的代码段大很多.来自Python,我想知道你是否可以简单地将整个数组乘以我下面的数字.
显然,它不像我在下面那样工作.有没有其他方法可以实现相同的功能,或者我必须像for循环一样遍历整个数组?
void main()
{
int i;
float data[] = {1.,2.,3.,4.,5.};
//this fails
data *= 5.0;
//this works
for(i = 0; i < 5; i++) data[i] *= 5.0;
}
Run Code Online (Sandbox Code Playgroud) 我写了一小段代码,试图遵守Fortran 2003标准.代码可以在github上找到.
这是我的makefile:
FC = gfortran
FLGS = -g -pg -std=f2003 -I. -fbounds-check
DEPS = camx.prm
OBJ = unit-test-hadvppm.o hadvppm.o
#linker macro
%.o: %.f03 $(DEPS)
$(FC) -c -o $@ $< $(FLGS)
#build targets
gnu-amd64-linux: $(OBJ)
$(FC) -o $@ $^ $(FLGS)
clean: gnu-amd64-linux
rm *.o
Run Code Online (Sandbox Code Playgroud)
使用上面的makefile和gfortran编译代码没有问题.
但是,如果我尝试用iFort编译它,只需使用
ifort -o ifort-amd64-linux unit-test-hadvppm.f03 hadvppm.f03
Run Code Online (Sandbox Code Playgroud)
它不起作用,请参阅下面的输出.我想这与.f03免费文件格式有关.iFort中有一个标志,类似于gfortran的-std = f2003标志吗?我试着在iFort文档中找到这个,我应该更加努力吗?

操作系统:Mac OS X.当我尝试运行下面的代码时,我收到错误:
ImportError:无法导入名称HeaderParsingError
我在代码下面附加了回溯.
我已经尝试使用谷歌和其他stackoverflow解决这个问题20分钟了.我试过跑:
pip install urllib3 --upgrade
我也尝试重新安装请求包.
它没有帮助.
这似乎是我的请求或urllib3包的问题.有没有人有类似的问题?
代码:
import requests
import json
def printResponse(r):
print '{} {}\n'.format(json.dumps(r.json(), sort_keys=True, indent=4, separators=(',', ': ')), r)
r = requests.get('http://wikitest.orcsoftware.com/rest/api/content',
params={'title': 'new page'},
auth=('seb', '****'))
printResponse(r)
parentPage = r.json()['results'][0]
pageData = {'type': 'comment', 'container': parentPage,
'body': {'storage': {'value': "<p>A new comment</p>", 'representation': 'storage'}}}
r = requests.post('http://localhost:8080/confluence/rest/api/content',
data=json.dumps(pageData),
auth=('admin', 'admin'),
headers=({'Content-Type': 'application/json'}))
printResponse(r)
Run Code Online (Sandbox Code Playgroud)
这是追溯:
Traceback (most recent call last):
File "/Users/sebastian/OneDrive/orc/restAPI/createSpace.py", line 1, in <module>
import requests
File "/Library/Python/2.7/site-packages/requests/__init__.py", …Run Code Online (Sandbox Code Playgroud) python ×4
c ×3
fortran ×2
arrays ×1
compilation ×1
d3.js ×1
for-loop ×1
javascript ×1
list ×1
math ×1
multiplying ×1
numpy ×1
openmp ×1
pointers ×1
python-2.7 ×1
syntax ×1
urllib3 ×1