0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「三角錐の座標による体積の公式」をChatGPTとsympyとFreeCADでやってみたい。

Last updated at Posted at 2024-05-21

・FreeCADは、勉強中です。
・三角錐の辺の長さによる、1/3✕底面積✕高さを使っていません。
 行列式です。

参考

ChatGPT-3.5先生へ(その1)

座標を使った三角錐の体積の公式を教えて下さい。

・(回答省略)

ChatGPT-3.5先生へ(その2) sympyで

座標を使った三角錐の体積の公式を教えて下さい。sympyのユーザー定義関数で。

・以下は、先生の出力コードです。
・このままで、うごきません。

import sympy as sp
def tetrahedron_volume(A, B, C, D):
# A, B, C, D are tuples representing the coordinates of the vertices
matrix = sp.Matrix([
[A[0], A[1], A[2], 1],
[B[0], B[1], B[2], 1],
[C[0], C[1], C[2], 1],
[D[0], D[1], D[2], 1]
])
# Calculate the determinant of the matrix
det = matrix.det()
# The volume of the tetrahedron is 1/6 the absolute value of the determinant
volume = sp.Abs(det) / 6
return volume
# Example usage
A = (0, 0, 0)
B = (1, 0, 0)
C = (0, 1, 0)
D = (0, 0, 1)
volume = tetrahedron_volume(A, B, C, D)
print(volume)
# ChatGPT の回答は必ずしも正しいとは限りません。重要な情報は確認するようにしてください。

修正も、先生におまかせしたらよかった。
・私が、手を加えています。
・私は、いつもですと、本当ですか??? 繰り返しています。

import sympy as sp
def tetrahedron_volume(A, B, C, D):
    # A, B, C, D are tuples representing the coordinates of the vertices
    matrix = sp.Matrix([
        [A[0], A[1], A[2], 1],
        [B[0], B[1], B[2], 1],
        [C[0], C[1], C[2], 1],
        [D[0], D[1], D[2], 1]
    ])
    # Calculate the determinant of the matrix
    det = matrix.det()
    # The volume of the tetrahedron is 1/6 the absolute value of the determinant
    volume = sp.Abs(det) / 6
    return volume
# Example usage
A = (0, 0, 0)
B = (1, 0, 0)
C = (0, 1, 0)
D = (0, 0, 1)
volume = tetrahedron_volume(A, B, C, D)
print("#",volume)
# 1/6

・以下は、気象大学のデータです。

import sympy as sp
def tetrahedron_volume(A, B, C, D):
    # A, B, C, D are tuples representing the coordinates of the vertices
    matrix = sp.Matrix([
        [A[0], A[1], A[2], 1],
        [B[0], B[1], B[2], 1],
        [C[0], C[1], C[2], 1],
        [D[0], D[1], D[2], 1]
    ])
    # Calculate the determinant of the matrix
    det = matrix.det()
    # The volume of the tetrahedron is 1/6 the absolute value of the determinant
    volume = sp.Abs(det) / 6
    return volume
# Example usage
A = (0,-1, 1)
B = (2, 0, 0)
C = (1, 0, 1)
D = (4, 2, 0)
volume = tetrahedron_volume(A, B, C, D)
print("#",volume)

sympyで、私のいつもの方法です。

・ver.0.1 : 三角錐の体積の公式
・安易に行列式を使っています。
>sympy docより
https://docs.sympy.org/latest/tutorials/intro-tutorial/matrices.html#advanced-methods

# ver0.1
from sympy import *
var('t'       ,real=True)
var('ax,ay,az',real=True)
var('bx,by,bz',real=True)
var('cx,cy,cz',real=True)
var('dx,dy,dz',real=True)
def SimentaiTaiseki(PTO,PTA,PTB,PTC):
    return Matrix([[PTA.x-PTO.x, PTA.y-PTO.y, PTA.z-PTO.z],
                   [PTB.x-PTO.x, PTB.y-PTO.y, PTB.z-PTO.z],
                   [PTC.x-PTO.x, PTC.y-PTO.y, PTC.z-PTO.z]]
                  ).det()/6
A=Point(ax,ay,az)
B=Point(bx,by,bz)
C=Point(cx,cy,cz)
D=Point(dx,dy,dz)
V=SimentaiTaiseki(A,B,C,D)
print("#",V)
# -ax*by*cz/6 + ax*by*dz/6 + ax*bz*cy/6 - ax*bz*dy/6 - ax*cy*dz/6 + ax*cz*dy/6 + ay*bx*cz/6 - ay*bx*dz/6 - ay*bz*cx/6 + ay*bz*dx/6 + ay*cx*dz/6 - ay*cz*dx/6 - az*bx*cy/6 + az*bx*dy/6 + az*by*cx/6 - az*by*dx/6 - az*cx*dy/6 + az*cy*dx/6 + bx*cy*dz/6 - bx*cz*dy/6 - by*cx*dz/6 + by*cz*dx/6 + bz*cx*dy/6 - bz*cy*dx/6

FreeCADのマクロで作図

勉強中

いつもの? sympyの実行環境と 参考のおすすめです。

(テンプレート)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?