www.91084.com

GVKun编程网logo

“ numpy.random.multivariate_normal”和“ numpy.random.Generator.multivariate_normal”之间的区别

3

想了解“numpy.random.multivariate_normal”和“numpy.random.Generator.multivariate_normal”之间的区别的新动态吗?本文将为您提供

想了解“ numpy.random.multivariate_normal”和“ numpy.random.Generator.multivariate_normal”之间的区别的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于@codechef - RNG@ Random Number Generator、cnpm install -g generator-gruntplugin & npm install -g generator-gruntplugin、Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate...、Java Random Random in interval [-1000,1000]的新知识。

本文目录一览:

“ numpy.random.multivariate_normal”和“ numpy.random.Generator.multivariate_normal”之间的区别

“ numpy.random.multivariate_normal”和“ numpy.random.Generator.multivariate_normal”之间的区别

如何解决“ numpy.random.multivariate_normal”和“ numpy.random.Generator.multivariate_normal”之间的区别?

我想知道“ numpy.random.multivariate_normal” https://numpy.org/doc/stable/reference/random/generated/numpy.random.multivariate_normal.html

之间的区别(如果有)

和numpy.random.Generator.multivariate_normal, https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.multivariate_normal.html#numpy.random.Generator.multivariate_normal

在第一个注释中说: “新代码应改用default_rng()实例的multivariate_normal方法;请参阅random-quick-start。” 并且 “ Generator.multivariate_normal 应该用于新代码。”

我都使用过:

mvn = np.random.default_rng()。multivariate_normal(Mu,Covar,200).T

mvn2 = np.random.multivariate_normal(Mu,Covar,200).T

,看不到任何区别。有什么区别吗?还是一种“方法”比其他更好?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

@codechef - RNG@ Random Number Generator

@codechef - RNG@ Random Number Generator

[toc]


@description@

给定递推关系式:$$A_i=C_1A_{i-1} + C_2A_{i-2}+\dots+C_kA_{i-k}$$ 并给定 $A_1, A_2, \dots , A_k$ 的值,求 $A_n$ 的值模 104857601。

input: 第一行给出两个整数 n,k。 第二行包含 k 个整数 A1,A2,...,Ak。 第三行包含 k 个整数 C1,C2,...,Ck。

output: 输出 An 的值。

constraints: 1 ≤ n ≤ 10^18 0 ≤ Ai, Ci < 104857601 1 ≤ k ≤ 30000

sample input: 3 5 1 2 3 4 5 6 sample output: 139 sample explanation: $A_1 = 1,A2 = 2,A3 = 3$ $A4 = (3 × 4 + 2 × 5 + 1 × 6) \mod 104857601 = 28$ $A5 = (28 × 4 + 3 × 5 + 2 × 6) \mod 104857601 = 139$

##@solution@ 这道题,如果你学过常系数齐次线性递推,就是一道模板题。

所以以下内容我就来 BB 什么是常系数齐次线性递推。

@part - 1@

解决这道题的经典算法就是矩阵乘法(也可以分治 FFT 或者多项式求逆)。

设出系数矩阵$$M = \begin{bmatrix} C_1&C_2&C_3&\cdots &C_{k-1}&C_k\ 1&0&0&\cdots&0&0\ 0&1&0&\cdots&0&0\ \vdots&\vdots&\vdots&\ddots&\vdots&\vdots \ 0&0&0&\cdots&1&0\ \end{bmatrix}$$ 并设 $B_i = \begin{bmatrix}A_{i+k-1}\A_{i+k-2}\\vdots\A_{i+1}\A_{i}\\end{bmatrix}$ 则:$$\begin{bmatrix}C_1&C_2&C_3&\cdots &C_{k-1}&C_k\1&0&0&\cdots&0&0\0&1&0&\cdots&0&0\\vdots&\vdots&\vdots&\ddots&\vdots&\vdots \ 0&0&0&\cdots&1&0\ \end{bmatrix} \begin{bmatrix}A_{i-1}\A_{i-2}\A_{i-3}\\vdots\A_{i-k}\\end{bmatrix}= \begin{bmatrix}A_{i}\A_{i-1}\A_{i-2}\\vdots\A_{i-k+1}\\end{bmatrix} $$

即 $MB_{i-k}=B_{i-k+1}$,或者等价地 $MB_{i}=B_{i+1}$

于是就有 $M^{n-1}B_1=B_n$,而 $B_1$ 是已知的,做一个矩阵快速幂求 $M^{n-1}$ 即可。 然而这样的复杂度为 $O(k^3\log n)$,对于这道题并不能通过。

我们接下来就来优化这个算法。

@part - 2@

【注:下面多项式中 x 并不是代表一个实数,而是一个什么都可以代表的东西。我一开始并没有理解,所以懵逼了好久 QAQ……】

定义多项式: $$f(x) = x^k-C_1x^{k-1}-C_2x^{k-2}-...-C_k$$ 通过一系列证明(证明我在下面part 3里面弄),发现: $$f(M) = 0$$ (右边那个 “0” 不是个数字,而是表示零矩阵)

我们用多项式除法,用 $x^n$ 除以 $f(x)$ 得到商式 $g(x)$ 与余式 $r(x)$。即: $$x^n=f(x)*g(x) + r(x)$$ 代入矩阵 M 得到: $$M^n=f(M)*g(M) + r(M)$$ 又因为 $f(M) = 0$,所以得到 $M^n=r(M)$。 其中多项式 $r(x)$ 的最高次数严格小于 k。

然后我们就把算法优化到 $O(k^4)$ 的了……?

我们再来看看我们要求解的是什么(下面那个 $r(M)$ 是 $M^{n-1}$ 的余式): $$B_n=M^{n-1}B_1 = r(M)B_1\ =r_0B_1+r_1MB_1+r_2M^2B_1+...+r_{k-1}M^{k-1}B_1\ =r_0B_1+r_1B_2+r_2B_{3}+...+r_{k-1}B_{k}$$

我们只需要 $B_n$ 中第最后一行的 $A_n$,所以有: $$A_n=r_0A_1+r_1A_2+r_2A_3+...+r_{k-1}A_k$$

时间复杂度在于多项式快速幂和取模 $O(k\log k\log n)$ 。 暴力写取模 $O(k^2\log n)$ ,以牺牲时间为代价降低代码复杂度,但是对于这道题而言是过不了的。

一个优化矩阵的算法最后代码全程不见矩阵……

@part - 3@

本节属于理性愉悦。

先给出几个概念吧: 特征值:若常数 $\lambda$ 对于矩阵 $M$,存在向量 $\vec x$ 使得: $$Mx = \lambda x$$ 则称 $\lambda$ 为矩阵 $M$ 的特征值。

变形上式得 $(\lambda I - M)x = 0$,该方程有解的充要条件为 $\det(\lambda I - M) = 0$。

特征多项式:$\det(\lambda I - M)$ 其实是一个多项式,我们称这个多项式为矩阵 M 的特征多项式,记为 $p(x)$。

Cayley - Hamilton 定理:$$p(M) = 0$$ 浅显,但并不易懂。该定理的证明超出了我的理解范围。所以这里并没有证明。 大家的数学知识如果比我不知道高到哪里去了,可以看这一篇博客。

对于矩阵 $M$,$$M = \begin{bmatrix} C_1&C_2&C_3&\cdots &C_{k-1}&C_k\ 1&0&0&\cdots&0&0\ 0&1&0&\cdots&0&0\ \vdots&\vdots&\vdots&\ddots&\vdots&\vdots \ 0&0&0&\cdots&1&0\ \end{bmatrix}$$ 如果要求解它的特征多项式,则: $$(\lambda I - M) = \begin{bmatrix} \lambda-C_1&-C_2&-C_3&\cdots &-C_{k-1}&-C_k\ -1&\lambda&0&\cdots&0&0\ 0&-1&\lambda&\cdots&0&0\ \vdots&\vdots&\vdots&\ddots&\vdots&\vdots \ 0&0&0&\cdots&-1&\lambda\ \end{bmatrix}$$ 以第一行展开求行列式,得到: $$\det(\lambda I - M) =(\lambda-C_1)M_{11}-C_2M_{12}..-C_kM_{1k}$$ 其中 $M_{ij}$ 是 $M$ 的代数余子式。

可以发现去掉第一行和第 i 列,剩下的矩阵是一个下三角矩阵。直接对角线相乘就可以求出行列式了。 所以: $$\det(\lambda I - M) =(\lambda-C_1)\lambda^{k-1}-C_2\lambda^{k-2}-...-C_k\=\lambda^k-C_1\lambda^{k-1}-C_2\lambda^{k-2}-...-C_k$$

结合上面的 Cayley - Hamilton 定理,我们就得到了我们想要的东西。

@accepted code@

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int G = 3;
const int MAXN = 30000*5;
const int MOD = 104857601;
int pow_mod(int b, int p) {
	int ret = 1;
	while( p ) {
		if( p & 1 ) ret = 1LL*ret*b%MOD;
		b = 1LL*b*b%MOD;
		p >>= 1;
	}
	return ret;
}
struct Polynomial{
	void poly_copy(int *A, int *B, int n) {
		for(int i=0;i<n;i++)
			A[i] = B[i];
	}
	void poly_clear(int *A, int l, int r) {
		for(int i=l;i<r;i++)
			A[i] = 0;
	}
	void poly_revcopy(int *A, int *B, int n) {
		for(int i=0;i<n;i++)
			A[i] = B[n-i-1];
	}
	void ntt(int *A, int n, int type) {
		for(int i=0,j=0;i<n;i++) {
			if( i < j ) swap(A[i], A[j]);
			for(int l=(n>>1);(j^=l)<l;l>>=1);
		}
		for(int s=2;s<=n;s<<=1) {
			int t = (s>>1);
			int u = (type == -1) ? pow_mod(G, (MOD-1) - (MOD-1)/s) : pow_mod(G, (MOD-1)/s);
			for(int i=0;i<n;i+=s) {
				for(int j=0,p=1;j<t;j++,p=1LL*p*u%MOD) {
					int x = A[i+j], y = 1LL*p*A[i+j+t]%MOD;
					A[i+j] = (x + y)%MOD, A[i+j+t] = (x + MOD - y)%MOD;
				}
			}
		}
		if( type == -1 ) {
			int inv = pow_mod(n, MOD-2);
			for(int i=0;i<n;i++)
				A[i] = 1LL*A[i]*inv%MOD;
		}
	}
	int tmp1[MAXN + 5], tmp2[MAXN + 5];
	void poly_mul(int *A, int *B, int *C, int n, int m) {
		int len; for(len = 1;len < n+m-1;len <<= 1);
		poly_copy(tmp1, A, n); poly_clear(tmp1, n, len);
		poly_copy(tmp2, B, m); poly_clear(tmp2, m, len);
		ntt(tmp1, len, 1); ntt(tmp2, len, 1);
		for(int i=0;i<len;i++)
			C[i] = 1LL*tmp1[i]*tmp2[i]%MOD;
		ntt(C, len, -1);
	}
	int tmp3[MAXN + 5];
	void poly_inv(int *A, int *B, int n) {
		if( n == 1 ) {
			B[0] = pow_mod(A[0], MOD-2);
			return ;
		}
		int len; for(len = 1;len < (n<<1);len <<= 1);
		poly_inv(A, B, (n + 1) >> 1);
		poly_copy(tmp3, A, n); poly_clear(tmp3, n, len);
		ntt(tmp3, len, 1); ntt(B, len, 1);
		for(int i=0;i<len;i++) B[i] = 1LL*B[i]*(2 + MOD - 1LL*tmp3[i]*B[i]%MOD)%MOD;
		ntt(B, len, -1); poly_clear(B, n, len);
	}
	int tmp4[MAXN + 5], tmp5[MAXN + 5], tmp6[MAXN + 5];
	void poly_mod(int *A, int *B, int *R, int n, int m) {
		poly_revcopy(tmp4, B, m); poly_clear(tmp5, 0, 2*(n-m+1)); poly_inv(tmp4, tmp5, n-m+1);
		poly_revcopy(tmp4, A, n); poly_mul(tmp4, tmp5, tmp6, n-m+1, n-m+1);
		
		poly_revcopy(tmp4, tmp6, n-m+1); poly_copy(tmp5, B, m);
		poly_mul(tmp4, tmp5, tmp6, n-m+1, m);
		for(int i=0;i<m-1;i++)
			R[i] = (A[i] + MOD - tmp6[i])%MOD;
	}
	int tmp7[MAXN + 5], tmp8[MAXN + 5];
	void poly_pow(int *A, int *B, int *mod, ll p, int n) {
		poly_copy(tmp7, A, n); B[0] = 1;
		while( p ) {
			if( p & 1 ) {
				poly_mul(B, tmp7, tmp8, n, n), poly_copy(B, tmp8, 2*n-1);
				poly_mod(B, mod, tmp8, 2*n-1, n), poly_copy(B, tmp8, n-1), poly_clear(B, n-1, 2*n-1);
			}
			poly_mul(tmp7, tmp7, tmp8, n, n), poly_copy(tmp7, tmp8, 2*n-1);
			poly_mod(tmp7, mod, tmp8, 2*n-1, n), poly_copy(tmp7, tmp8, n-1), poly_clear(tmp7, n-1, 2*n-1);
			p >>= 1;
		} 
	}
}poly;
int A[MAXN + 5], C[MAXN + 5];
int f[MAXN + 5], p[MAXN + 5], r[MAXN + 5];
int main() {
	ll N; int K;
	scanf("%d%lld", &K, &N);
	for(int i=1;i<=K;i++)
		scanf("%d", &A[i]);
	for(int i=1;i<=K;i++)
		scanf("%d", &C[i]);
	for(int i=0;i<K;i++)
		f[i] = (MOD - C[K-i]);
	f[K] = p[1] = 1;
	poly.poly_pow(p, r, f, N-1, K+1);
	int ans = 0;
	for(int i=0;i<K;i++)
		ans = (ans + 1LL*r[i]*A[i+1]%MOD)%MOD;
	printf("%d\n", ans);
}

@details@

注意因为要多次使用某些数组,所以用完就清是个好习惯~~(然后就被卡常)~~。 余式的次数是严格小于除式的次数,不能取等。

cnpm install -g generator-gruntplugin & npm install -g generator-gruntplugin

cnpm install -g generator-gruntplugin & npm install -g generator-gruntplugin

npm WARN deprecated CSSselect@0.4.1: the module is Now available as 'css-select'
npm WARN deprecated CSSwhat@0.4.7: the module is Now available as 'css-what'
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated minimatch@0.2.14: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated graceful-fs@1.2.3: graceful-fs v3.0.0 and before will fail on node releases >= v7.0. Please update to graceful-fs@^4.0.0 as soon as possible. Use 'npm ls graceful-fs' to find it in the tree.
npm WARN checkPermissions Missing write access to D:\Program Files\nodejs\node_modules\generator-gruntplugin\node_modules\inquirer\node_modules\ansi-regex
npm WARN generator-gruntplugin@0.0.9 requires a peer of yo@>=1.0.0 but none was installed.

npm ERR! path D:\Program Files\nodejs\node_modules\generator-gruntplugin\node_modules\inquirer\node_modules\ansi-regex
npm ERR! code ENOENT
npm ERR! errno -4058
npm ERR! syscall access
npm ERR! enoent ENOENT: no such file or directory,access 'D:\Program Files\nodejs\node_modules\generator-gruntplugin\node_modules\inquirer\node_modules\ansi-regex'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\yizhichao\AppData\Roaming\npm-cache\_logs\2017-07-06T10_32_27_053Z-debug.log

cnpm isntall -g  CSSselect@0.4.1

cnpm isntall -g CSSwhat@0.4.7


cnpm isntall -g minimatch@3.0.2


cnpm isntall -g graceful-fs@^4.0.0


cnpm isntall -g  generator-gruntplugin@1.0.0

npm ls graceful-fs

Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate...

Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate...

springboot整合mybatis时出现的问题

解决方法:在pom.xml中plugin中加入单独依赖Mysql驱动包,问题便可解决

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <!--加入下面这个依赖-->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                </dependencies>

            </plugin>

 

Java Random Random in interval [-1000,1000]

Java Random Random in interval [-1000,1000]

在 java我有:

Random random = new Random();
double randomNum = random.nextDouble();

这会创建一个介于0和1之间的随机数.但是我想要一个介于-1000和1000之间的数字,我该如何缩放呢?

谢谢

解决方法

2种可能性:

> [密度较小]:将结果乘以2000,然后减去1000
从结果.它不会像可能性2那样“密集”.
>在范围[-1000,999]中获取随机int,并添加一个随机双精度
范围[0,1].

请注意,可能性2确保了更好的随机性和更好的“密度”,以2次随机调用为代价[如果这是一个问题,这可能是扩展性的].

今天关于“ numpy.random.multivariate_normal”和“ numpy.random.Generator.multivariate_normal”之间的区别的讲解已经结束,谢谢您的阅读,如果想了解更多关于@codechef - RNG@ Random Number Generator、cnpm install -g generator-gruntplugin & npm install -g generator-gruntplugin、Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate...、Java Random Random in interval [-1000,1000]的相关知识,请在本站搜索。

本文标签: