-
最近の投稿
- 再帰の基礎:八元数、二分木、Cohl Furey のモデル組み込み 実行結果とソースコード & 描画2
- 再帰の基礎:八元数、二分木、Cohl Furey のモデル組み込み 実行結果とソースコード & 描画
- 【99%の人か゛見れない】あなたの魂レヘ゛ルか゛最高位に到達。前代未聞な報酬を受け取って下さい。 spirutual letter プレアデスの啓示
- 【二状態ベクトル】あなたが明日おこなう測定が、今この電子の状態を後ろから決めている COSMIENCE コスミエンス
- 【限定配信】※名指しで指名されました。あなたに緊急の直接通知が届いています。宇宙の総力を挙げた大転換が今日から始まります。【プレアデスからのメッセージ】 プレアデス最高評議会
最近のコメント
- 気象改変 ケムトレイル HAARP GEOEngineering 気候変動 NWO 過疎化 に 180user より
- THE HIDDEN HISTORY OF THE KHAZARIAN MAFIA に 180user より
- 習近平国賓招聘反対国民総決起大会 1万人大行進 に 180user より
- 習近平国賓招聘反対国民総決起大会 1万人大行進 に 小森谷 祐子 より
- Hello world! に WordPress コメントの投稿者 より
アーカイブ
- 2026年7月
- 2026年6月
- 2026年5月
- 2026年4月
- 2026年3月
- 2026年2月
- 2026年1月
- 2025年12月
- 2025年11月
- 2025年10月
- 2025年9月
- 2025年8月
- 2025年7月
- 2025年6月
- 2025年5月
- 2025年4月
- 2025年3月
- 2025年2月
- 2025年1月
- 2024年12月
- 2024年11月
- 2024年10月
- 2024年9月
- 2024年8月
- 2024年7月
- 2024年6月
- 2024年5月
- 2024年4月
- 2024年3月
- 2024年2月
- 2024年1月
- 2023年12月
- 2023年11月
- 2023年10月
- 2023年9月
- 2023年8月
- 2023年7月
- 2023年6月
- 2023年5月
- 2023年4月
- 2023年3月
- 2023年2月
- 2023年1月
- 2022年12月
- 2022年11月
- 2022年10月
- 2022年9月
- 2022年8月
- 2022年7月
- 2022年6月
- 2022年5月
- 2022年4月
- 2022年3月
- 2022年2月
- 2022年1月
- 2021年12月
- 2021年11月
- 2021年10月
- 2021年9月
- 2021年8月
- 2021年7月
- 2021年6月
- 2021年5月
- 2021年4月
- 2021年3月
- 2021年2月
- 2021年1月
- 2020年12月
- 2020年11月
- 2020年10月
- 2020年9月
- 2020年8月
- 2020年7月
- 2020年6月
- 2020年5月
- 2020年4月
- 2020年3月
- 2020年2月
- 2020年1月
- 2019年11月
- 2019年10月
- 2019年9月
- 2019年8月
- 2019年7月
- 2019年6月
カテゴリー
メタ情報
再帰の基礎:2倍に分岐増殖する様子を再帰モデルで表現する 実行結果とソースコード
C++Builder2007ソースコード
//************************
//***** Unit1.h ********
//************************
//—————————————————————————
#ifndef Unit1H
#define Unit1H
//—————————————————————————
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include “Chart.hpp”
#include “TeEngine.hpp”
#include “TeeProcs.hpp”
#include <ExtCtrls.hpp>
#include “Series.hpp”
#include <vector>
//—————————————————————————
const int max_depth = 8; // 最大階層数
class RecursiveWave
{
public:
int current_depth; // クラスメンバーとして
double Value;
RecursiveWave *child1, *child2;
RecursiveWave *z_1, *z_2;
int direction; // +1: 順方向、 -1:逆方向
// RecursiveWaveクラスに追加(public) 逆転版(トップダウン用)
bool isTopDownMode;
RecursiveWave(int n, int dir);
~RecursiveWave();
void LinkParents(RecursiveWave* parent1, RecursiveWave* parent2); // 後からリンク
void SetupParents(RecursiveWave* node, RecursiveWave* p1, RecursiveWave* p2);
void Dump(TMemo *memo);
double GetValue(){ return Value; }
};
std::vector<RecursiveWave*> all_waves1;
std::vector<RecursiveWave*> all_waves2;
//—————————————————————————
class TForm1 : public TForm
{
__published: // IDE-managed Components
TChart *Chart1;
TButton *ButtonExit;
TLineSeries *Series1;
TMemo *Memo1;
TLineSeries *Series2;
TButton *Button1;
TRadioGroup *RadioGroup1;
void __fastcall ButtonExitClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
RecursiveWave *Top;
void ChartSetting();
};
//—————————————————————————
extern PACKAGE TForm1 *Form1;
//—————————————————————————
#endif
//************************
//***** Unit1.cpp ******
//************************
//—————————————————————————
#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include “Unit1.h”
//—————————————————————————
#pragma package(smart_init)
#pragma link “Chart”
#pragma link “TeEngine”
#pragma link “TeeProcs”
#pragma link “Series”
#pragma resource “*.dfm”
TForm1 *Form1;
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//—————————————————————————
void __fastcall TForm1::ButtonExitClick(TObject *Sender)
{
Close();
}
//—————————————————————————
// ***** ChartSetting *****
void TForm1::ChartSetting()
{
Chart1->SeriesList->ClearValues();
// 軸タイトルだけ設定(SeriesCountチェックはButton1Click側に移動)
Chart1->LeftAxis->Title->Caption = “Value1”;
Chart1->LeftAxis->Title->Visible = true;
Chart1->RightAxis->Title->Caption = “Value2”;
Chart1->RightAxis->Title->Visible = true;
Chart1->RightAxis->Visible = true;
Chart1->BottomAxis->Title->Caption = “Count”;
Chart1->BottomAxis->Visible = true;
}
// 構築後に親を設定する専用メソッド
void RecursiveWave::LinkParents(RecursiveWave* parent1, RecursiveWave* parent2) {
z_1 = parent1;
z_2 = parent2 ? parent2 : parent1; // z_2がなければz_1で代用
// Valueを親を使って計算
if (z_1) {
Value = z_1->Value * 1.7 – (z_2 ? z_2->Value * 0.97 : 0.0) + 1.0;
} else {
Value = 1.0; // 最上位の初期値
}
}
// 新規ヘルパー関数(トップダウンで親情報を伝播)
void RecursiveWave::SetupParents(RecursiveWave* node, RecursiveWave* p1, RecursiveWave* p2) {
if (!node) return;
node->LinkParents(p1, p2);
//node->LinkParents();
// 子に伝える(z_1 = node自身、z_2 = p1)
SetupParents(node->child1, node, p1); //再帰呼び出し
SetupParents(node->child2, node, p1); //再帰呼び出し
}
// ***** 順方向:逆方向選択版 コンストラクタ *****
RecursiveWave::RecursiveWave(int n, int dir): current_depth(n), Value(0.0), child1(NULL), child2(NULL),
z_1(NULL), z_2(NULL), direction(dir)
{
if (n >= max_depth) return;
if(direction == -1){
//逆方向
// 子を先に作成(再帰)
child1 = new RecursiveWave(n + 1, dir);
child2 = new RecursiveWave(n + 1, -dir);
// all_waves登録
all_waves1.push_back(this);
all_waves2.push_back(this);
}else if(direction == +1){
//順方向
// all_wavesに先に登録
all_waves1.push_back(this);
all_waves2.push_back(this);
// 子を先に作成(再帰)
child1 = new RecursiveWave(n + 1, dir);
child2 = new RecursiveWave(n + 1, -dir);
}
}
//**********************
//デストラクタ
RecursiveWave::~RecursiveWave()
{
if (child1 != NULL) {
delete child1; // 再帰的に子孫をすべて削除
child1 = NULL;
}
if (child2 != NULL) {
delete child1; // 再帰的に子孫をすべて削除
child1 = NULL;
}
// all_wavesから自分を除去(任意)
// (vectorからeraseするのは重いので、クリア時に一括処理する方が良い)
}
void RecursiveWave::Dump(TMemo *memo)
{
if (all_waves1.empty()) return;
memo->Hide();
AnsiString str;
int count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves1.begin();
it != all_waves1.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, neigh->current_depth, neigh->Value) );
count++;
}
}
memo->Show();
/*memo->Lines->Add(“==============”);
for(int i=0; i<all_waves->size(); i++){
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, i, (*all_waves)[i].Value ) );
}*/
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 既存のオブジェクトをクリア(メモリリーク防止)
for (size_t i = 0; i < all_waves1.size(); i++) {
delete all_waves1[i];
}
all_waves1.clear();
// 既存のオブジェクトをクリア(メモリリーク防止)
for (size_t i = 0; i < all_waves2.size(); i++) {
delete all_waves2[i];
}
all_waves2.clear();
//方向指定、初期化、生成
RecursiveWave *WaveTop;
if(RadioGroup1->ItemIndex == 1){
//順方向
WaveTop = new RecursiveWave(0, +1); // 引数不要!
}else if(RadioGroup1->ItemIndex == 0){
//逆方向
WaveTop = new RecursiveWave(0, -1); // 引数不要!
}
// 再帰的に親リンクを設定(トップダウン)
WaveTop->SetupParents(WaveTop, NULL, NULL); // 新規ヘルパー関数
WaveTop->Dump(Memo1);
ChartSetting();
// Seriesを安全に追加
Chart1->SeriesList->ClearValues(); // 念のため
// Series[0] と Series[1] が確実に存在することを保証
while (Chart1->SeriesCount() < 2) {
TLineSeries* s = new TLineSeries(Chart1);
Chart1->AddSeries(s);
}
int count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves1.begin();
it != all_waves1.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
Chart1->Series[0]->AddY(neigh->Value, “”, clBlue); // Value(左軸)
//Chart1->Series[0]->AddY(neigh->current_depth, “”, clBlue); // Depth(右軸) ← コメント解除OK
count++;
}
}
count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves2.begin();
it != all_waves2.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
Chart1->Series[1]->AddY(neigh->Value, “”, clRed); // Value(左軸)
//Chart1->Series[1]->AddY(neigh->current_depth, “”, clRed); // Depth(右軸) ← コメント解除OK
count++;
}
}
// 軸設定を再適用
Chart1->Series[0]->VertAxis = 0; // vaLeft
Chart1->Series[1]->VertAxis = 1; // vaRight
delete WaveTop;
all_waves1.clear();
all_waves2.clear();
}
//—————————————————————————
再帰の基礎:波を再帰モデルで表現する 逆転版(過去→現在)を追加
実行結果
C++Builder2007ソースコード
//************************
//***** Unit1.h ********
//************************
//—————————————————————————
#ifndef Unit1H
#define Unit1H
//—————————————————————————
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include “Chart.hpp”
#include “TeEngine.hpp”
#include “TeeProcs.hpp”
#include <ExtCtrls.hpp>
#include “Series.hpp”
#include <vector>
//—————————————————————————
const int max_depth = 100; // 最大階層数
class RecursiveWave
{
public:
int current_depth; // クラスメンバーとして
double Value;
RecursiveWave *child;
RecursiveWave *z_1, *z_2;
int direction; // +1: 順方向、 -1:逆方向
// RecursiveWaveクラスに追加(public) 逆転版(トップダウン用)
bool isTopDownMode;
RecursiveWave(int n, int dir);
void LinkParents(RecursiveWave* parent1, RecursiveWave* parent2); // 後からリンク
void Dump(TMemo *memo);
double GetValue(){ return Value; }
};
std::vector<RecursiveWave*> all_waves;
//—————————————————————————
class TForm1 : public TForm
{
__published: // IDE-managed Components
TChart *Chart1;
TButton *ButtonExit;
TLineSeries *Series1;
TMemo *Memo1;
TLineSeries *Series2;
TButton *Button1;
TRadioGroup *RadioGroup1;
void __fastcall ButtonExitClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
RecursiveWave *Top;
void ChartSetting();
};
//—————————————————————————
extern PACKAGE TForm1 *Form1;
//—————————————————————————
#endif
//************************
//***** Unit1.cpp ******
//************************
//—————————————————————————
#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include “Unit1.h”
//—————————————————————————
#pragma package(smart_init)
#pragma link “Chart”
#pragma link “TeEngine”
#pragma link “TeeProcs”
#pragma link “Series”
#pragma resource “*.dfm”
TForm1 *Form1;
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//—————————————————————————
void __fastcall TForm1::ButtonExitClick(TObject *Sender)
{
Close();
}
//—————————————————————————
// ChartSetting
void TForm1::ChartSetting()
{
Chart1->SeriesList->ClearValues();
// 軸タイトルだけ設定(SeriesCountチェックはButton1Click側に移動)
Chart1->LeftAxis->Title->Caption = “Value”;
Chart1->LeftAxis->Title->Visible = true;
Chart1->RightAxis->Title->Caption = “Depth”;
Chart1->RightAxis->Title->Visible = true;
Chart1->RightAxis->Visible = true;
Chart1->BottomAxis->Title->Caption = “Count”;
Chart1->BottomAxis->Visible = true;
}
// ***** 双方向版 *****
RecursiveWave::RecursiveWave(int n, int dir): current_depth(n), Value(0.0), child(NULL),
z_1(NULL), z_2(NULL), direction(dir)
{
if (n >= max_depth) return;
if(direction == -1){
// 子を先に作成(再帰)
child = new RecursiveWave(n + 1, dir);
// all_waves登録
all_waves.push_back(this);
}else if(direction == +1){
// all_wavesに先に登録
all_waves.push_back(this);
// 子を先に作成(再帰)
child = new RecursiveWave(n + 1, dir);
}
}
// 新規ヘルパー関数(トップダウンで親情報を伝播)
void SetupParents(RecursiveWave* node, RecursiveWave* p1, RecursiveWave* p2) {
if (!node) return;
node->LinkParents(p1, p2);
// 子に伝える(z_1 = node自身、z_2 = p1)
SetupParents(node->child, node, p1);
}
// 構築後に親を設定する専用メソッド
void RecursiveWave::LinkParents(RecursiveWave* parent1, RecursiveWave* parent2) {
z_1 = parent1;
z_2 = parent2 ? parent2 : parent1; // z_2がなければz_1で代用
// Valueを親を使って計算
if (z_1) {
Value = z_1->Value * 1.7 – (z_2 ? z_2->Value * 0.97 : 0.0) + 1.0;
} else {
Value = 1.0; // 最上位の初期値
}
}
//**********************
void RecursiveWave::Dump(TMemo *memo)
{
if (all_waves.empty()) return;
AnsiString str;
int count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves.begin();
it != all_waves.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, neigh->current_depth, neigh->Value) );
count++;
}
}
/*memo->Lines->Add(“==============”);
for(int i=0; i<all_waves->size(); i++){
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, i, (*all_waves)[i].Value ) );
}*/
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 既存のオブジェクトをクリア(メモリリーク防止)
for (size_t i = 0; i < all_waves.size(); i++) {
delete all_waves[i];
}
all_waves.clear();
//方向指定、初期化、生成
RecursiveWave *WaveTop;
if(RadioGroup1->ItemIndex == 1){
//順方向
WaveTop = new RecursiveWave(0, +1); // 引数不要!
}else if(RadioGroup1->ItemIndex == 0){
//逆方向
WaveTop = new RecursiveWave(0, -1); // 引数不要!
}
// 再帰的に親リンクを設定(トップダウン)
SetupParents(WaveTop, NULL, NULL); // 新規ヘルパー関数
WaveTop->Dump(Memo1);
ChartSetting();
// Seriesを安全に追加
Chart1->SeriesList->ClearValues(); // 念のため
// Series[0] と Series[1] が確実に存在することを保証
while (Chart1->SeriesCount() < 2) {
TLineSeries* s = new TLineSeries(Chart1);
Chart1->AddSeries(s);
}
int count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves.begin();
it != all_waves.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
Chart1->Series[0]->AddY(neigh->Value, “”, clBlue); // Value(左軸)
Chart1->Series[1]->AddY(neigh->current_depth, “”, clRed); // Depth(右軸) ← コメント解除OK
count++;
}
}
// 軸設定を再適用
Chart1->Series[0]->VertAxis = 0; // vaLeft
Chart1->Series[1]->VertAxis = 1; // vaRight
}
//—————————————————————————
再帰の基礎:波を再帰モデルで表現する 実行結果とソースコード
実行結果
Memo1
depth= 99, Value= 4.1556
depth= 98, Value= 3.88514
depth= 97, Value= 3.55581
depth= 96, Value= 3.25747
depth= 95, Value= 3.0741
depth= 94, Value= 3.06032
depth= 93, Value= 3.2252
depth= 92, Value= 3.52836
depth= 91, Value= 3.88971
depth= 90, Value= 4.21046
depth= 89, Value= 4.40008
depth= 88, Value= 4.40172
depth= 87, Value= 4.20912
depth= 86, Value= 3.86988
depth= 85, Value= 3.47389
depth= 84, Value= 3.12963
depth= 83, Value= 2.93451
depth= 82, Value= 2.94746
depth= 81, Value= 3.17132
depth= 80, Value= 3.55028
depth= 79, Value= 3.98368
depth= 78, Value= 4.35254
depth= 77, Value= 4.55221
depth= 76, Value= 4.52188
depth= 75, Value= 4.26286
depth= 74, Value= 3.84019
depth= 73, Value= 3.36646
depth= 72, Value= 2.97195
depth= 71, Value= 2.76892
depth= 70, Value= 2.81981
depth= 69, Value= 3.1183
depth= 68, Value= 3.58898
depth= 67, Value= 4.10614
depth= 66, Value= 4.52728
depth= 65, Value= 4.7322
depth= 64, Value= 4.65718
depth= 63, Value= 4.31443
depth= 62, Value= 3.79109
depth= 61, Value= 3.22724
depth= 60, Value= 2.77857
depth= 59, Value= 2.57354
depth= 58, Value= 2.67675
depth= 57, Value= 3.069
depth= 56, Value= 3.65006
depth= 55, Value= 4.26401
depth= 54, Value= 4.741
depth= 53, Value= 4.944
depth= 52, Value= 4.80805
depth= 51, Value= 4.36049
depth= 50, Value= 3.71628
depth= 49, Value= 3.04864
depth= 48, Value= 2.54269
depth= 47, Value= 2.34426
depth= 46, Value= 2.5181
depth= 45, Value= 3.02732
depth= 44, Value= 3.74057
depth= 43, Value= 4.46561
depth= 42, Value= 5.001
depth= 41, Value= 5.19185
depth= 40, Value= 4.97437
depth= 39, Value= 4.39648
depth= 38, Value= 3.60787
depth= 37, Value= 2.82156
depth= 36, Value= 2.25647
depth= 35, Value= 2.07674
depth= 34, Value= 2.34432
depth= 33, Value= 2.99856
depth= 32, Value= 3.86931
depth= 31, Value= 4.7209
depth= 30, Value= 5.31568
depth= 29, Value= 5.48017
depth= 28, Value= 5.15526
depth= 27, Value= 4.41627
depth= 26, Value= 3.45607
depth= 25, Value= 2.53511
depth= 24, Value= 1.91094
depth= 23, Value= 1.76649
depth= 22, Value= 2.15679
depth= 21, Value= 2.98975
depth= 20, Value= 4.0472
depth= 19, Value= 5.04174
depth= 18, Value= 5.6946
depth= 17, Value= 5.81348
depth= 16, Value= 5.34879
depth= 15, Value= 4.41181
depth= 14, Value= 3.24875
depth= 13, Value= 2.17635
depth= 12, Value= 1.49593
depth= 11, Value= 1.409
depth= 10, Value= 1.95811
depth= 9, Value= 3.0101
depth= 8, Value= 4.28768
depth= 7, Value= 5.44222
depth= 6, Value= 6.14856
depth= 5, Value= 6.19621
depth= 4, Value= 5.55155
depth= 3, Value= 4.3726
depth= 2, Value= 2.971
depth= 1, Value= 1.73
depth= 0, Value= 1
C++Builder2007ソースコード
//************************
//***** Unit1.h ********
//************************
//—————————————————————————
#ifndef Unit1H
#define Unit1H
//—————————————————————————
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include “Chart.hpp”
#include “TeEngine.hpp”
#include “TeeProcs.hpp”
#include <ExtCtrls.hpp>
#include “Series.hpp”
#include <vector>
//—————————————————————————
const int max_depth = 100; // 最大階層数
class RecursiveWave
{
public:
int current_depth; // クラスメンバーとして
double Value;
RecursiveWave *child;
RecursiveWave *z_1, *z_2;
RecursiveWave(int n);
void LinkParents(RecursiveWave* parent1, RecursiveWave* parent2); // 後からリンク
void Dump(TMemo *memo);
double GetValue(){ return Value; }
};
std::vector<RecursiveWave*> all_waves;
//—————————————————————————
class TForm1 : public TForm
{
__published: // IDE-managed Components
TChart *Chart1;
TButton *ButtonExit;
TLineSeries *Series1;
TMemo *Memo1;
TLineSeries *Series2;
TButton *Button1;
void __fastcall ButtonExitClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
RecursiveWave *Top;
void ChartSetting();
};
//—————————————————————————
extern PACKAGE TForm1 *Form1;
//—————————————————————————
#endif
//************************
//***** Unit1.cpp ******
//************************
//—————————————————————————
#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include “Unit1.h”
//—————————————————————————
#pragma package(smart_init)
#pragma link “Chart”
#pragma link “TeEngine”
#pragma link “TeeProcs”
#pragma link “Series”
#pragma resource “*.dfm”
TForm1 *Form1;
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//—————————————————————————
void __fastcall TForm1::ButtonExitClick(TObject *Sender)
{
Close();
}
//—————————————————————————
// ChartSetting
void TForm1::ChartSetting()
{
Chart1->SeriesList->ClearValues();
// 軸タイトルだけ設定(SeriesCountチェックはButton1Click側に移動)
Chart1->LeftAxis->Title->Caption = “Value”;
Chart1->LeftAxis->Title->Visible = true;
Chart1->RightAxis->Title->Caption = “Depth”;
Chart1->RightAxis->Title->Visible = true;
Chart1->RightAxis->Visible = true;
Chart1->BottomAxis->Title->Caption = “Count”;
Chart1->BottomAxis->Visible = true;
}
RecursiveWave::RecursiveWave(int n): current_depth(n), Value(0.0), child(NULL),
z_1(NULL), z_2(NULL)
{
if (n >= max_depth) return;
// 子を先に作成(再帰)
child = new RecursiveWave(n + 1);
// all_waves登録
all_waves.push_back(this);
}
// 構築後に親を設定する専用メソッド
void RecursiveWave::LinkParents(RecursiveWave* parent1, RecursiveWave* parent2) {
z_1 = parent1;
z_2 = parent2 ? parent2 : parent1; // z_2がなければz_1で代用
// Valueを親を使って計算
if (z_1) {
Value = z_1->Value * 1.7 – (z_2 ? z_2->Value * 0.97 : 0.0) + 1.0;
} else {
Value = 1.0; // 最上位の初期値
}
}
void RecursiveWave::Dump(TMemo *memo)
{
if (all_waves.empty()) return;
AnsiString str;
int count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves.begin();
it != all_waves.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, neigh->current_depth, neigh->Value) );
count++;
}
}
/*memo->Lines->Add(“==============”);
for(int i=0; i<all_waves->size(); i++){
memo->Lines->Add(str.sprintf(“depth= %d, Value= %g”, i, (*all_waves)[i].Value ) );
}*/
}
// 新規ヘルパー関数(トップダウンで親情報を伝播)
void SetupParents(RecursiveWave* node, RecursiveWave* p1, RecursiveWave* p2) {
if (!node) return;
node->LinkParents(p1, p2);
// 子に伝える(z_1 = node自身、z_2 = p1)
SetupParents(node->child, node, p1);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 既存のオブジェクトをクリア(メモリリーク防止)
for (size_t i = 0; i < all_waves.size(); i++) {
delete all_waves[i];
}
all_waves.clear();
// 正しい初期化
RecursiveWave *WaveTop = new RecursiveWave(0); // 引数不要!
// 再帰的に親リンクを設定(トップダウン)
SetupParents(WaveTop, NULL, NULL); // 新規ヘルパー関数
WaveTop->Dump(Memo1);
ChartSetting();
// Seriesを安全に追加
Chart1->SeriesList->ClearValues(); // 念のため
// Series[0] と Series[1] が確実に存在することを保証
while (Chart1->SeriesCount() < 2) {
TLineSeries* s = new TLineSeries(Chart1);
Chart1->AddSeries(s);
}
int count = 0;
for (std::vector<RecursiveWave*>::const_iterator it = all_waves.begin();
it != all_waves.end(); ++it) {
RecursiveWave* neigh = *it;
if (neigh) {
Chart1->Series[0]->AddY(neigh->Value, “”, clBlue); // Value(左軸)
Chart1->Series[1]->AddY(neigh->current_depth, “”, clRed); // Depth(右軸) ← コメント解除OK
count++;
}
}
// 軸設定を再適用
Chart1->Series[0]->VertAxis = 0; // vaLeft
Chart1->Series[1]->VertAxis = 1; // vaRight
}
//—————————————————————————
Z変換
うさぎでもわかる信号処理 第01羽 z変換のいろは
https://www.momoyama-usagi.com/entry/math-seigyo01#gsc.tab=0



