将单元段递归分为两部分(2-D情况) [英] Splitting the unit segment into two parts recursively (2-D case)

查看:78
本文介绍了将单元段递归分为两部分(2-D情况)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我问了一个问题有关如何在1-D和I中生成简单的多重分形(二项测度)的问题收到了一个很好的答案.现在,我想生成二维二项式测度,但是在实现该功能时遇到了困难.

Recently I have asked a question about how to generate a simple multifractal (Binomial Measure) in 1-D and I received a nice answer. Now, I would like to generate 2-D Binomial Measure but I have faced difficulties in implementing this.

一维情况可以生成如下:

A 1-D case can be generated as follows:

二项式度量是通过递归构造方便地定义的概率度量.首先将$ I:= [0,1] $分为两个等长的子间隔$ I_0 $和$ I_1 $,并为其分配质量$ m_0 $和$ m_1 = 1-m_0 $.对于这两个子间隔,一个以相同的方式进行,依此类推:在第二阶段,例如四个子间隔$ I_ {00},I_ {01},I_ {10},I_ {11} $分别具有质量$ m_0m_0,m_0m_1 m_1m_0 ​​m_1m_1 $.

The binomial measure is a probability measure which is defined conveniently via a recursive construction. Start by splitting $ I := [0, 1] $ into two subintervals $ I_0 $ and $ I_1 $ of equal length and assign the masses $ m_0 $ and $ m_1 = 1 - m_0 $ to them. With the two subintervals one proceeds in the same manner and so forth: at stage two, e.g. the four subintervals $ I_{00}, I_{01}, I_{10}, I_{11} $ have masses $ m_0m_0, m_0m_1 m_1m_0 m_1m_1 $ respectively.

Rudolf H. Riedi.多重分形简介

直观上来说,二维泛化似乎很容易.在二维中,我们没有单位区间的间隔,而是有一个单位正方形,该正方形递归地分为四个子正方形,质量为m0,m1,m2,m3(给定m0 + m1 + m2 + m3 = 1),然后分成16个子间隔,即64等

Intuitively the generalization in 2-D seems easy. Instead of intervals on a unit segment, in 2-D we have a unit square, that recursively splits into four subsquares with masses m0, m1, m2, m3 given m0 + m1 + m2 + m3 = 1, then into 16 subintervals, 64, etc.

MWE(一维情况):

MWE (1-D case):

import matplotlib.pyplot as plt
from sympy import var

def binom_measuare(iterations, p=0.4, current_layer=None):

    var('m0 m1')

    if current_layer is None:
        current_layer = [1]

    next_layer = []

    for item in current_layer:
        next_layer.append(m0*item)
        next_layer.append(m1*item)

    if iterations != 0:
        return binom_measuare(iterations - 1, current_layer=next_layer)

    else:
        return [i.subs(m0, p).subs(m1, 1 - p) for i in next_layer]

让我们绘制输出

y = binom_measuare(iterations=12)
x = [(i+1) / len(y) for i in range(len(y))]

x = [0] + x
y = [0] + y

plt.plot(x, y)

二维情况下的MWE:

def binom_measuare_2D(iterations, p1=0.4, p2=0.2, p3=0.3, p4=0.1, current_layer=None):

    var('mx0 mx1 my0 my1')

    if current_layer is None:
        current_layer = [1]

    next_layer = []

    for item in current_layer:
        next_layer.append(mx0*item)
        next_layer.append(mx1*item)
        next_layer.append(my0*item)
        next_layer.append(my1*item)

    if iterations != 0:
        return binom_measuare_2D(iterations-1, p1, p2, p3, p4, next_layer)

    else:
        return [i.subs(mx0, p1).subs(mx1, p2).subs(my0, p3).subs(my1, p4) for i in next_layer]

使用示例:

import numpy as np

iterat = 1
z = binom_measuare_2D(iterations=iterat, p1=0.1, p2=0.2, p3=0.3, p4=0.4)
z = np.array(z)
z = z.reshape(2**(1 + iterat), 2**(1 + iterat))
print(z)

似乎在输出中有正确的值,但我不知道如何从列表中为每个坐标x和y赋值.它返回子正方形的值,但不返回一般正方形的行的值,因此可以很容易地看到它.让我说明一下我的意思.在上面提供的示例中,令(行索引; col索引)为输出数组的索引.

It seems to have right values in the output but I have no idea how to assign values from the list for each coordinate x and y. It returns the values for subsquare, but not for the row of the general square so it can be easily visualized. Let me illustrate what I mean. Let (row index; col index) be the indices of the output array in the example provided above.

它按照以下模式填充:

但是,我希望它能像这样被填充:

However, I expect it to be filled like this one:

推荐答案

与我在上一个答案中写的类似,我建议,而不是考虑拆分单元格,而是考虑用四个新单元格替换每个单元格.的双有丝分裂.可以像这样计算新单元格中的值,不同的是,将对结果进行整形以形成4x4矩阵.通过迭代当前层的单元并将4x4结果放入下一代层,可以从当前层计算出下一代层.

In analogy to what I wrote in a previous answer I would suggest, rather than thinking in terms of splitting cells consider replacing each cell with four new cells, a kind of double mitosis. The values in the new cells can be calculated like this except that the result would be reshaped to form a 4x4 matrix. The next generation layer would be calculated from the current layer by iterating through the cells of the current layer and placing the 4x4 results into the next generation layer.

>>> from sympy import *
>>> var('m0 m1 m2 m3')
(m0, m1, m2, m3)
>>> def mitosis(aCell):
...     return ((m0**3, m0**2*m1, m0*m1**2, m1**3), (m0**2*m2, m0*m1*m2, m0*m1*m3, m1**2*m3), (m0*m2**2, m0*m2*m3, m1*m2*m3, m1*m3**2), (m0**3, m2**2*m3, m2*m3**2, m3**3))
... 
>>> first = (m0,m1,m2,m3)
>>> mitosis(first)
((m0**3, m0**2*m1, m0*m1**2, m1**3), (m0**2*m2, m0*m1*m2, m0*m1*m3, m1**2*m3), (m0*m2**2, m0*m2*m3, m1*m2*m3, m1*m3**2), (m0**3, m2**2*m3, m2*m3**2, m3**3))

这篇关于将单元段递归分为两部分(2-D情况)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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