介紹
將大十進制數相乘可能在計算上具有挑戰性,尤其是在處理具有許多位數或多個小數位的數字時。傳統的乘法方法對於極大的數字來說效率很低。這就是快速傅立葉變換 (FFT) 發揮作用的地方,它提供了一種強大而高效的演算法,可以以驚人的速度進行大數相乘。
乘法中的應用
- FFT 透過將數字轉換到頻域、執行逐點乘法,然後應用逆 FFT 來實現多項式或大整數的快速乘法。
大數乘法的挑戰
傳統乘法方法的時間複雜度為 O(n²),其中 n 是位數。對於非常大的數字,這在計算上變得昂貴。基於 FFT 的乘法演算法將這種複雜性降低到 O(n log n),使得處理大數的速度顯著加快。
Cooley-Tukey FFT 的證明大綱
-
離散傅立葉轉換 (DFT) 的分解:
- DFT 定義為:
X_k = sum_{n=0}^{N-1} x_n cdot e^{-2pi i cdot kn / N}, Xk = n=0ΣN−1 xn e−2πi⋅kn /N, 在哪裡 NN
-
是輸入訊號的大小。
N/2 N/2透過分離偶數索引和奇數索引項: Xk k k k k k k k > >= n=0ΣN/2−1 xC🎜>> −2πi⋅(2n)k/Nn=0
-
⋅ e。 這減少為: Xk=k==> 🎜> Wk⋅DFT 的奇數項>DFT 的奇數項🎜> X_k = text{偶數項的DFT} W_k cdot text{奇數項的DFT}, Xk =DFT 的偶數項 k> 🎜>⋅奇數項的DFT, 在哪裡 Wk =e−2πi ⋅k/N
.
- DFT 定義為:
-
遞歸結構
:- 每個大小的 DFT NNN/2 N/2
- ,導致遞歸結構。 N = 1 N=1
-
,此時 DFT 只是輸入值。
- 蝴蝶操作: v,a' = u W_k cdot v,四邊形 ue - W_b' = u - W_k c u - W_k c , 一個′=u Wkkk >⋅v,b =u−WW 🎜>k⋅v,⋅v,v, 在哪裡 uuuv v 是較小 DFT 的結果, WkW_k Wk
- 蝴蝶操作
-
-
代表團結的根源。
位元反轉排列: 輸入數組根據索引的二進位表示重新排序,以實現就地計算。 時間複雜度:- 在每一層遞迴中,都有 NN log_2(N) log2 (N)
- (N) .
- O( 逆 FFT 類似,但使用 e2π2π ⋅ke^{2pi i> kn / N} e
- 2πi⋅kn/N
-
- 作為基礎並對結果進行縮放
-
1
- /
- N
- 1/N
-
1/N
- .
- 了解 FFT 乘法演算法
FFT 乘法演算法的工作原理有幾個關鍵步驟: -
- 將相乘後的陣列轉換回時域
- 處理數位進位
- 重構最終的十進制數
實施的關鍵組成部分
複數表示
class Complex { constructor(re = 0, im = 0) { this.re = re; // Real part this.im = im; // Imaginary part } // Static methods for complex number operations static add(a, b) { /* ... */ } static subtract(a, b) { /* ... */ } static multiply(a, b) { /* ... */ } }
Complex 類別對於執行 FFT 運算至關重要,它使我們能夠操作實域和虛域中的數字。
快速傅立葉變換函數
function fft(a, invert = false) { // Bit reversal preprocessing // Butterfly operations in frequency domain // Optional inverse transformation }
FFT函數是演算法的核心,有效地在時域和頻域之間進行數位轉換。
處理小數
此實作包含處理十進制數的複雜邏輯:
- 分隔整數和小數部分
- 追蹤小數點總數
- 使用正確的小數點位置重建結果
範例用例
// Multiplying large integers fftMultiply("12345678901234567890", "98765432109876543210") // Multiplying very large different size integers fftMultiply("12345678901234567890786238746872364872364987293795843790587345", "9876543210987654321087634875782369487239874023894") // Multiplying decimal numbers fftMultiply("123.456", "987.654") // Handling different decimal places fftMultiply("1.23", "45.6789") // Handling different decimal places with large numbers fftMultiply("1234567890123456789078623874687236487236498.7293795843790587345", "98765432109876543210876348757823694.87239874023894")
性能優勢
- 時間複雜度:O(n log n) 與傳統方法的 O(n²) 相比
- 精度:處理具有多個小數位的極大數字
- 效率:大量乘法運算速度明顯加快
限制和注意事項
- 需要額外的記憶體來表示複數
- 浮點運算會影響精確度
- 與傳統乘法相比,實現更複雜
結論
FFT 乘法演算法代表了一種有效地乘以大數的強大方法。透過利用頻域變換,我們可以以驚人的速度和精度執行複雜的數學運算。
實際應用
- 科學計算
- 財務計算
- 密碼學
- 大規模數值模擬
進一步閱讀
- Cooley-Tukey FFT 演算法
- 數論
- 計算數學
程式碼
完整的實作如下,為使用快速傅立葉變換方法乘以大十進制數提供了一個強大的解決方案。
/** * Fast Fourier Transform (FFT) implementation for decimal multiplication * @param {number[]} a - Input array of real numbers * @param {boolean} invert - Whether to perform inverse FFT * @returns {Complex[]} - Transformed array of complex numbers */ class Complex { constructor(re = 0, im = 0) { this.re = re; this.im = im; } static add(a, b) { return new Complex(a.re + b.re, a.im + b.im); } static subtract(a, b) { return new Complex(a.re - b.re, a.im - b.im); } static multiply(a, b) { return new Complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re); } } function fft(a, invert = false) { let n = 1; while (n > 1; for (; j & bit; bit >>= 1) { j ^= bit; } j ^= bit; if (i > 1; for (let i = 0; i { const [intPart, decPart] = numStr.split("."); return { intPart: intPart || "0", decPart: decPart || "", totalDecimalPlaces: (decPart || "").length, }; }; const parsed1 = parseNumber(num1); const parsed2 = parseNumber(num2); // Combine numbers removing decimal point const combinedNum1 = parsed1.intPart + parsed1.decPart; const combinedNum2 = parsed2.intPart + parsed2.decPart; // Total decimal places const totalDecimalPlaces = parsed1.totalDecimalPlaces + parsed2.totalDecimalPlaces; // Convert to digit arrays (least significant first) const a = combinedNum1.split("").map(Number).reverse(); const b = combinedNum2.split("").map(Number).reverse(); // Determine result size and pad const resultSize = a.length + b.length; const fftSize = 1 new Complex(x, 0)); const complexB = b.map((x) => new Complex(x, 0)); // Perform FFT const fftA = fft(complexA); const fftB = fft(complexB); // Pointwise multiplication in frequency domain const fftProduct = new Array(fftSize); for (let i = 0; i = 10) { result[i + 1] += Math.floor(result[i] / 10); result[i] %= 10; } } // Remove leading zeros and convert to string while (result.length > 1 && result[result.length - 1] === 0) { result.pop(); } // Insert decimal point const resultStr = result.reverse().join(""); if (totalDecimalPlaces === 0) { return resultStr; } // Handle case where result might be shorter than decimal places if (resultStr.length <h3> 輸出 </h3> <pre class="brush:php;toolbar:false">// Example Usage - Self verify using Python console.log( "Product of integers:", fftMultiply("12345678901234567890", "98765432109876543210") ); console.log("Product of decimals:", fftMultiply("123.456", "987.654")); console.log("Product of mixed decimals:", fftMultiply("12.34", "56.78")); console.log( "Product with different decimal places:", fftMultiply("1.23", "45.6789") ); console.log( "Product with large integers:", fftMultiply( "12345678901234567890786238746872364872364987293795843790587345", "9876543210987654321087634875782369487239874023894" ) ); const num1 = "1234567890123456789078623874687236487236498.7293795843790587345"; const num2 = "98765432109876543210876348757823694.87239874023894"; console.log("Product:", fftMultiply(num1, num2));
Product of integers: 1219326311370217952237463801111263526900 Product of decimals: 121931.812224 Product of mixed decimals: 700.6652 Product with different decimal places: 56.185047 Product with large integers: 121932631137021795232593613105722759976860134207381319681901040774443113318245930967231822167723255326824021430 Product: 121932631137021795232593613105722759976860134207381319681901040774443113318245.93096723182216772325532682402143
以上是使用快速傅立葉變換 (FFT) 乘以大十進制數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

10款趣味橫生的jQuery遊戲插件,讓您的網站更具吸引力,提升用戶粘性!雖然Flash仍然是開發休閒網頁遊戲的最佳軟件,但jQuery也能創造出令人驚喜的效果,雖然無法與純動作Flash遊戲媲美,但在某些情況下,您也能在瀏覽器中獲得意想不到的樂趣。 jQuery井字棋遊戲 遊戲編程的“Hello world”,現在有了jQuery版本。 源碼 jQuery瘋狂填詞遊戲 這是一個填空遊戲,由於不知道單詞的上下文,可能會產生一些古怪的結果。 源碼 jQuery掃雷遊戲

本教程演示瞭如何使用jQuery創建迷人的視差背景效果。 我們將構建一個帶有分層圖像的標題橫幅,從而創造出令人驚嘆的視覺深度。 更新的插件可與JQuery 1.6.4及更高版本一起使用。 下載

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

Matter.js是一個用JavaScript編寫的2D剛體物理引擎。此庫可以幫助您輕鬆地在瀏覽器中模擬2D物理。它提供了許多功能,例如創建剛體並為其分配質量、面積或密度等物理屬性的能力。您還可以模擬不同類型的碰撞和力,例如重力摩擦力。 Matter.js支持所有主流瀏覽器。此外,它也適用於移動設備,因為它可以檢測觸摸並具有響應能力。所有這些功能都使其值得您投入時間學習如何使用該引擎,因為這樣您就可以輕鬆創建基於物理的2D遊戲或模擬。在本教程中,我將介紹此庫的基礎知識,包括其安裝和用法,並提供一

本文演示瞭如何使用jQuery和ajax自動每5秒自動刷新DIV的內容。 該示例從RSS提要中獲取並顯示了最新的博客文章以及最後的刷新時間戳。 加載圖像是選擇


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Dreamweaver Mac版
視覺化網頁開發工具