20

SQLite データベースでユークリッド距離を計算する必要があります。

数学関数の動的ライブラリを作成してロードする以外に、sqlite で平方根を計算する方法を知っている人はいますか?

ここhttp://en.wikipedia.org/wiki/Fast_inverse_square_rootの高速逆平方根アルゴリズムに頼ろうとしていますが、今必要以上に楽しくなる可能性があります。

補足として、べき乗を行う方法を理解することは素晴らしいことです (これは一般化された質問であり、数値をそれ自体で乗算するよりもクリーンなコーディングです)。

ありがとう、

シモーネ

4

6 に答える 6

7

さて、私は半分の答えを持っています。

はい、サードパーティが関与していますが、自分で記述する必要はありません。このページの最後の拡張機能を確認しましたか?

いくつかの数学関数が含まれており、その中には sqrt() があります。

于 2012-10-04T16:48:08.147 に答える
6

警告:この回答はコーディング言語に依存しています。私の場合、C# .

ユーザー定義の SQLite 関数は、実装するのが面倒でした。最後に、長い間検索した結果、C# コードに実装することができました。メイン関数は次のようになります。

[SQLiteFunction(Arguments = 1, FuncType = FunctionType.Scalar, Name = "Sqrt")]
class Sqrt : SQLiteFunction
{
    public override object Invoke(object[] args)
    {
        return Math.Sqrt(Double.Parse(args[0].ToString()));
    }
}

カスタム関数の登録:

SQLiteFunction.RegisterFunction(typeof(Sqrt));

そして選択で使用:

SQLiteCommand com = new SQLiteCommand("select sqrt(10.42)", connection);

ここから完全な例をダウンロードできます: http://db.tt/qzeNXwso

または、コードのみを表示したい (またはコードのすべての部分を確認したい) 場合は、SQLite データベースで平方根を計算するための完全に機能するサンプル コードを以下に貼り付けます。この例を作成して実行するには、次の 6 つの手順を実行します。

  1. 新しいプロジェクトを作成します (私の名前は Sqrt です)
  2. プロジェクトへの SQLite 参照を含めます:
    ソリューション エクスプローラー -> 参照 (右クリック: 参照の追加) -> アセンブリ - 拡張機能 - System.Data.SQLite (チェック) -> OK
  3. App.configを開き、これに置き換えます (この手順を行わないと、混合モードのアセンブリエラーが発生する可能性があります)

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
    </startup>
    </configuration>

  4. Form1.Designer.csを次のコードに置き換えます。

    namespace Sqrt
    {
     partial class Form1
     {
     /// <summary>
     /// Required designer variable.
     /// </summary>
     private System.ComponentModel.IContainer components = null;
    
     /// <summary>
     /// Clean up any resources being used.
     /// </summary>
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
     protected override void Dispose(bool disposing)
     {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
     }
    
     #region Windows Form Designer generated code
    
     /// <summary>
     /// Required method for Designer support - do not modify
     /// the contents of this method with the code editor.
     /// </summary>
     private void InitializeComponent()
     {
        this.txb_Input = new System.Windows.Forms.TextBox();
        this.txb_Output = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.btn_Calcualte = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // txb_Input
        // 
        this.txb_Input.Location = new System.Drawing.Point(131, 12);
        this.txb_Input.Name = "txb_Input";
        this.txb_Input.Size = new System.Drawing.Size(201, 20);
        this.txb_Input.TabIndex = 0;
        // 
        // txb_Output
        // 
        this.txb_Output.BackColor = System.Drawing.Color.WhiteSmoke;
        this.txb_Output.Location = new System.Drawing.Point(131, 38);
        this.txb_Output.Name = "txb_Output";
        this.txb_Output.ReadOnly = true;
        this.txb_Output.Size = new System.Drawing.Size(201, 20);
        this.txb_Output.TabIndex = 0;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 15);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(31, 13);
        this.label1.TabIndex = 1;
        this.label1.Text = "Input";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(12, 41);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(39, 13);
        this.label2.TabIndex = 1;
        this.label2.Text = "Output";
        // 
        // btn_Calcualte
        // 
        this.btn_Calcualte.Location = new System.Drawing.Point(257, 64);
        this.btn_Calcualte.Name = "btn_Calcualte";
        this.btn_Calcualte.Size = new System.Drawing.Size(75, 23);
        this.btn_Calcualte.TabIndex = 2;
        this.btn_Calcualte.Text = "Calculate";
        this.btn_Calcualte.UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(344, 98);
        this.Controls.Add(this.btn_Calcualte);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.txb_Output);
        this.Controls.Add(this.txb_Input);
        this.Name = "Form1";
        this.Text = "Root square example";
        this.ResumeLayout(false);
        this.PerformLayout();
     }
     #endregion
    
     private System.Windows.Forms.TextBox txb_Input;
     private System.Windows.Forms.TextBox txb_Output;
     private System.Windows.Forms.Label label1;
     private System.Windows.Forms.Label label2;
     private System.Windows.Forms.Button btn_Calcualte;
     }
    }
    
  5. Form1.cs (コード) を開き、コードを次のように置き換えます。System.Data.SQLite の使用; System.Windows.Forms を使用します。

    namespace Sqrt
    {
        // definition of custom sqlite function
        [SQLiteFunction(Arguments = 1, FuncType = FunctionType.Scalar, Name = "Sqrt")]
        class Sqrt : SQLiteFunction
        {
            public override object Invoke(object[] args)
            {
                return Math.Sqrt(Double.Parse(args[0].ToString())); // return result of math sqrt function
            }
        }
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.btn_Calcualte.Click += new System.EventHandler(this.btn_Calcualte_Click);
            }
    
            private void btn_Calcualte_Click(object sender, EventArgs e)
            {
                if (txb_Input.Text.Length == 0)
                    return;
                try { SQLiteConnection.CreateFile(AppDomain.CurrentDomain.BaseDirectory + "test.s3db"); }
                catch { }
                SQLiteConnection con = new SQLiteConnection("Data Source=test.s3db");
                SQLiteFunction.RegisterFunction(typeof(Sqrt)); // register custom function
                con.Open();
                SQLiteCommand com = new SQLiteCommand("select sqrt(" + txb_Input.Text.Replace(',', '.') + ")", con); // select result
                string res = com.ExecuteScalar().ToString();
                txb_Output.Text = res;
            }
        }
    }
    
  6. 走って、試して、楽しんでください。

于 2013-05-28T10:22:17.290 に答える
2

私の知る限り、コア機能だけを使用してそれを行うことはできません。

ネイティブ関数コア関数のリストと集計関数集計関数のリストを次に示します。

問題を解決するには、ここに示すように独自のUDF (ユーザー定義関数) を記述します。

于 2012-10-04T16:53:19.400 に答える