如何进行随机蒙特卡洛扫描并在数据文件中获取输出 [英] How to do a random Monte Carlo scan and getting the outputs in a data file

查看:99
本文介绍了如何进行随机蒙特卡洛扫描并在数据文件中获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些python程序,比如说'Test.py',它有一个类,并且在init里面必须引入三个随机变量.

I have some python program let's say 'Test.py' it has a class and inside this in the init I have to introduce three random variables.

import math
class Vector():
def __init__(self,vx,vy,vz):
    self.x=vx
    self.y=vy
    self.z=vz

def norm(self):
    xx=self.x**2
    yy=self.y**2
    zz=self.z**2
    return math.sqrt(xx+yy+zz)

现在,我制作了一个运行文件"Testrun.py",该文件将调用该文件,然后为每个数据集生成一个结果

Now I made a run file 'Testrun.py' which calls this file and then for each dataset it produces one result

import math
import numpy as np

from Desktop import Test
def random_range(n, min, max):
   return min + np.random.random(n) * (max - min)

model=Test.Vector(x,y,z)

x=random_range(20,2,9)
y=random_range(20,2,9)
z=random_range(20,2,9)

trial_args = np.stack((x, y, z), axis=-1)
for x, y, z in trial_args:
   print(x, y, z, '=>', model.norm())

现在我只想存储给出'norm'> 5的结果,并想将输入和输出打印在数据文件中

Now I want to store only the results which gives the 'norm'>5 and want to print the inputs and outputs in a data file

推荐答案

保持相同的"Test.py"

Keeping the same 'Test.py'

import math
class Vector():
 def __init__(self,vx,vy,vz):
    self.x=vx
    self.y=vy
    self.z=vz

 def norm(self):
    xx=self.x**2
    yy=self.y**2
    zz=self.z**2
    return math.sqrt(xx+yy+zz)

我们必须通过if循环放置输出条件

We have to put the output condition by a if loop

import math
import numpy as np

from Desktop import Test
def random_range(n, min, max):
return min + np.random.random(n) * (max - min)

x=random_range(20,2,9)
y=random_range(20,2,9)
z=random_range(20,2,9)

trial_args = np.stack((x, y, z), axis=-1)
for x, y, z in trial_args:
    model=Test.Vector(x,y,z)
    if model.norm()>5:
       print(x, y, z, '=>', model.norm())

这篇关于如何进行随机蒙特卡洛扫描并在数据文件中获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆