我试图在6参数函数的参数空间上运行以研究它的数值行为,然后再尝试做任何复杂的事情,所以我正在寻找一种有效的方法来做到这一点.
给定6-dim numpy数组作为输入,我的函数采用浮点值.我最初尝试做的是:
首先,我创建了一个函数,它接受2个数组并生成一个数组,其中包含来自两个数组的所有值组合
from numpy import *
def comb(a,b):
c = []
for i in a:
for j in b:
c.append(r_[i,j])
return c
Run Code Online (Sandbox Code Playgroud)
然后我习惯reduce()将它应用于相同数组的m个副本:
def combs(a,m):
return reduce(comb,[a]*m)
Run Code Online (Sandbox Code Playgroud)
然后我评估我的功能如下:
values = combs(np.arange(0,1,0.1),6)
for val in values:
print F(val)
Run Code Online (Sandbox Code Playgroud)
这有效,但它太慢了.我知道参数的空间很大,但这不应该太慢.在这个例子中我只抽取了10 6(一百万)个点,并且创建数组花了超过15秒values.
你知道用numpy做这个更有效的方法吗?
F如果有必要,我可以修改函数获取它的参数的方式.
我在关于haskell和函数式编程的博客中经常阅读这个术语(特别是在sigfpe的博客中),但我不清楚这是什么意思.我大部分时间都不知道,但如果我知道,我可能会更好地理解文本.谷歌没有帮助我.我迷失在技术方面.
此外,世界的非技术含义("改变抽象具体")并没有帮助我理解在代码中实现某些东西的实际意义.
我对计算机科学概念有点慢,所以使用代码的实际例子会很好.:P
haskell functional-programming terminology metaprogramming reification
对于变量z的许多不同值,我有两个变量(x,y)的一系列线图.我通常会添加带有这样的图例的线条图:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
# suppose mydata is a list of tuples containing (xs, ys, z)
# where xs and ys are lists of x's and y's and z is a number.
legns = []
for(xs,ys,z) in mydata:
pl = ax.plot(xs,ys,color = (z,0,0))
legns.append("z = %f"%(z))
ax.legends(legns)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是我有太多的图表,传说将涵盖图表.我宁愿用一个颜色条来表示与颜色对应的z的值.我无法在galery中找到类似的东西,我所有的尝试都处理了colorbar失败.显然我必须在尝试添加颜色条之前创建一组绘图.
是否有捷径可寻?谢谢.
编辑(澄清):
我想做这样的事情:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
fig = plt.figure()
ax = fig.add_subplot(111)
mycmap = cm.hot
# suppose mydata is …Run Code Online (Sandbox Code Playgroud) 关于使用以下模式是否有任何缺点,警告或不良做法警告?
def buildString(user, name = 'john', age=22):
userId = user.getUserId()
return "Name: {name}, age: {age}, userid:{userId}".format(**locals())
Run Code Online (Sandbox Code Playgroud)
我有一个非常重复的字符串生成代码来编写并且很想使用它,但是关于使用的东西locals()让我感到不舒服.这有意外行为的危险吗?
编辑:上下文
我发现自己经常写下这样的东西:
"{name} {age} {userId} {etc}...".format(name=name, age=age, userId=userId, etc=etc)
Run Code Online (Sandbox Code Playgroud) 假设我有以下课程:
class MyInteger {
private:
int n_;
public:
MyInteger(int n) : n_(n) {};
// MORE STUFF
};
Run Code Online (Sandbox Code Playgroud)
并且假设这个类没有默认的普通构造函数MyInteger().int出于某种原因,我必须始终提供初始化它.然后假设在我的代码中的某个地方我需要一个vector<MyInteger>.如何MyInteger在此初始化每个组件vector<>?
我有两种情况(可能解决方案是相同的,但无论如何我都会说明),一个函数内的正常变量:
int main(){
vector<MyInteger> foo(10); //how do I initialize each
//MyInteger field of this vector?
doStuff(foo);
}
Run Code Online (Sandbox Code Playgroud)
并作为班级中的数据:
class MyFunClass {
private:
vector<MyInteger> myVector;
public:
MyFunClass(int size, int myIntegerValue) : myVector(size) {};
// what do I put here if I need the
// initialization to call MyInteger(myIntegerValue) for all
// components of …Run Code Online (Sandbox Code Playgroud) 我正在制作一个与此类似的代码:
#include <stdio.h>
double some_function( double x, double y)
{
double inner_function(double x)
{
// some code
return x*x;
}
double z;
z = inner_function(x);
return z+y;
}
int main(void)
{
printf("%f\n", some_function(2.0, 4.0));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在GCC中完美编译(没有警告)但无法在ICC中编译.
ICC给出:
main.c(16): error: expected a ";"
{
^
main.c(21): warning #12: parsing restarts here after previous syntax error
double z;
^
main.c(22): error: identifier "z" is undefined
z = inner_function(x);
^
compilation aborted for main.c (code 2)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
谢谢.
(编辑)抱歉这个可怜的例子.在我的原始代码中,我有点需要做这些事情.我正在使用GSL数值积分器,有类似的东西:
double stuff(double …Run Code Online (Sandbox Code Playgroud) 在一些书籍中,经常在互联网上,我看到像" operator==应该被宣布为朋友"这样的建议.
当运营商必须被宣布为朋友以及何时应该被宣布为会员时,我应该如何理解?除了==和之外,最常需要被宣布为朋友的运营商是什么<<?
我最近开始阅读有关Go编程语言的内容,我发现通道变量是一个非常吸引人的概念.是否可以在Haskell中模拟相同的概念?也许有一个数据类型Channel a和一个monad结构来启用可变状态和函数,就像关键字一样go.
我在并发编程方面不是很擅长,而在Haskell中这样简单的通道传递机制会让我的生活更轻松.
编辑
人们让我澄清了我对转换为Haskell感兴趣的Go模式.所以Go的通道变量是第一类的,可以传递并由函数返回.我可以读取和写入这些通道,因此可以在可以并发运行的例程之间轻松进行通信.Go还有一个go关键字,根据语言规范同时启动函数执行作为独立线程,并继续执行代码而无需等待.
我感兴趣的是确切的模式是这样的(Go的语法很奇怪 - 变量由varName的VARTYPE而不是通常的倒方式声明的 - 但我认为这是可读的):
func generateStep(ch chan int) {
//ch is a variable of type chan int, which is a channel that comunicate integers
for {
ch <- randomInteger() //just sends random integers in the channel
}
func filter(input, output chan int) {
state int
for {
step <- input //reads an int from the input channel
newstate := update(state, step) //update the variable with some update function …Run Code Online (Sandbox Code Playgroud) 我有一些对象要发送到我的应用程序上的芹菜任务.这些对象显然不是使用默认json库的json序列化.有没有办法让芹菜使用自定义JSON Encoder/ 序列化/反序列化这些对象Decoder?
我正在尝试学习STL库,我遇到了一个奇怪的问题.此代码完美编译:
void Show(vector<int> myvec)
{
vector<int>::iterator it;
cout << "Vector contains:";
for( it = myvec.begin(); it < myvec.end(); it++)
{
cout << " " << *it;
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
虽然这个在编译时给我一个错误信息:
template <class T>
void Show2(vector<T> myvec)
{
vector<T>::iterator it;
cout << "Vector contains:";
for( it = myvec.begin(); it < myvec.end(); it++)
{
cout << " " << *it;
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
错误是:
$ g++ hello.cpp
hello.cpp: In function ‘void Show2(std::vector<T, std::allocator<_Tp1> >)’:
hello.cpp:19: error: expected ‘;’ …Run Code Online (Sandbox Code Playgroud)