识别圆锥部分


13

在二维平面上给定5个不同的点,确定由这些点形成的圆锥截面的类型。输出应为一个circlehyperbolaellipse,或parabola

规则

  • 这些点将处于大致线性位置,这意味着没有三个点是共线的,因此通过它们的圆锥将是唯一的。
  • 5点的坐标将是介于-10和10之间(含10和10)的十进制数字。
  • 十进制/浮点型值的精度应为您语言的本机浮点/十进制类型的精度。如果您的语言/数据类型是任意精度,则您可以使用小数点后的12位数字作为所需的最大精度,并四舍五入为零(例如1.0000000000005 == 1.000000000000)。
  • 输出的大小写无关紧要。
  • 输出ellipse时,圆锥曲线实际上是一个圆圈是不允许的。所有圆都是椭圆,但是您必须输出最具体的一个。

关于浮点数的误差和精度:

我正在尝试使其尽可能简单,以免浮点数不准确的问题受到阻碍。我们的目标是,如果数据类型是“神奇的无限精度值”,而不是浮点数/双精度数,那么一切都会正常运行。但是,由于“神奇的无限精度值”不存在,因此您编写的代码假定您的值是无限精度,并且由于浮点错误而出现的任何问题都是功能,而不是错误。

测试用例

(0, 0), (1, 5), (2, 3), (4, 8), (9, 2) => hyperbola
(1.2, 5.3), (4.1, 5.6), (9.1, 2.5), (0, 1), (4.2, 0) => ellipse
(5, 0), (4, 3), (3, 4), (0, 5), (0, -5) => circle
(1, 0), (0, 1), (2, 1), (3, 4), (4, 9) => parabola

2
对于浮点数,输出circle似乎需要检查浮点相等性以区别于非常椭圆的椭圆。我们在这里应该假设什么精度?
xnor

1
@Mego为什么不允许所有语言都使用整数形式的问题,但是范围应更广,例如-10000到
10000。– orlp

1
您确定测试用例四正确吗?desmos:desmos.com/calculator/fmwrjau8fd
Maltysen,2016年

2
另外,3看起来也错了:desmos.com/calculator/tkx1wrkotd
Maltysen

1
我认为您不满意FP精度的问题,并且导致像这样的答案codegolf.stackexchange.com/a/77815/21348
edc65 '16

Answers:


2

Matlab,154个字节

p=input();c=null([p.^2 prod(p,2) p 1+p(:,1)*0]),s={'circle' 'ellipse' 'parabola' 'hyperbola'};s{3+sign(c(3)^2-4*c(1)*c(2))-~max(abs(c(3)),abs(c(1)-c(2)))}

由于Suever的建议,节省了一些字节。

将输入作为[x1 y1;x2 y2;x3 y3; etc]。这使用了Vandermonde矩阵,并找到了其零空间的基础,该零空间将始终是单个向量。然后,它计算判别式,并使用它来确定介于1和4之间的索引,该索引用于获取字符串。

取消高尔夫:

p=input();
c=null([p.^2 prod(p')' p ones(length(p),1)]);
s={'circle' 'ellipse' 'parabola' 'hyperbola'};
s{3+sign(c(3)^2-4*c(1)*c(2))-~max(abs(c(3)),abs(c(1)-c(2)))}

sign(...)部分计算判别式,如果为正(双曲线),则为1;如果为负(椭圆),则为-1;如果为0(抛物线),则为0。在max(...)减去1的路程,如果它是一个圆。Matlab数组是单索引的,因此加3即可得到值1、2、3、4,并使用该值对圆锥截面名称数组进行索引。


1
max() == 0~max()
无需

1
另外,ones(length(p),1)您也不能这样做1+p(:,1)*0
Suever 2016年

干杯,这max()件事对我来说很愚蠢,我以前在那里做过比较,显然很懒!这种获取方法ones也非常好。
大卫

14

JavaScript(ES6),316 323 347

p=>[1,2,4].some(x=>(d=D(Q=[[x&1,x&2,x&4,0,0,0],...p.map(([x,y])=>[x*x,x*y,y*y,x,y,1])]))?[a,b,c]=Q.map((v,i)=>D(Q.map((r,j)=>(r=[...r],r[i]=x*!j,r)))/d):0,D=m=>m[1]?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0):m)&&(d=b*b-4*a*c)?d<0?!b&c==a?'Circle':'Ellipse':'Hyperbola':'Parabola'

任何更适合处理矩阵和行列式的语言都应得分更高(APL,J,CJAM,Jelly)

参考文献:圆锥的一般形式五点确定圆锥线性方程组行列式

在笛卡尔平面中,圆锥的一般方程为

A*x*x + B*x*y + C*y*y + D*x + E*y + F = 0 

A或B或C不等于0(否则为直线)

A ... F是六个未知数。使用五对(x,y),我们可以构建具有五个方程式的线性系统,然后缩放移除一个维度。也就是说,如果A,B或C中的一个不为0,则可以将其设置为1(并且我们知道至少一个不为0)。

我构建并尝试解决3个系统:首先尝试A = 1。如果无法解决,则B = 1,然后C。(可能有更好的方法,但这是我当时的最佳方法)

有了A,B,C的值,我们可以根据判别式对圆锥进行分类 d=B*B-4*A*C

  • d == 0->抛物线
  • d> 0->双曲线
  • d <0->椭圆,特别是(A == C和B == 0)->圆

少打高尔夫球

F=p=>(
  // Recursive function to find determinant of a square matrix
  D=m=>m[1]
    ?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0)
    :m,
  // Try 3 linear systems, coefficients in Q
  // Five equation made from the paramaters in p
  // And a first equation with coefficient like k,0,0,0,0,0,1 (example for A)  
  [1,2,4].some(
    x => (
      // matrix to calc the determinant, last coefficient is missing at this stage
      Q = [ 
        [x&1, x&2, x&4, 0,0,0] // first one is different
        // all other equations built from the params 
        ,...p.map( ([x,y]) => [x*x, x*y, y*y, x, y, 1] )
      ],
      d = D(Q), // here d is the determinant
      d && ( // if solvable  then d != 0
        // add missing last coefficient to Q
        // must be != 0 for the first row, must be 0 for the other
        Q.map( r=> (r.push(x), x=0) ),
        // solve the system (Cramer's rule), I get all values for A...F but I just care of a,b,c
        [a,b,c] = Q.map((v,i)=>D(Q.map(r=>(r=[...r],r[i]=r.pop(),r))) / d),
        d = b*b - 4*a*c, // now reuse d for discriminant
        d = d<0 ? !b&c==a ? 'Circle' : 'Ellipse' // now reuse d for end result
        : d ? 'Hyperbola' : 'Parabola'
      ) // exit .some if not 0
    ), d // .some exit with true, the result is in d
  )  
)

测试

F=p=>[1,2,4].some(x=>(d=D(Q=[[x&1,x&2,x&4,0,0,0],...p.map(([x,y])=>[x*x,x*y,y*y,x,y,1])]))?[a,b,c]=Q.map((v,i)=>D(Q.map((r,j)=>(r=[...r],r[i]=x*!j,r)))/d):0,D=m=>m[1]?m[0].reduce((r,v,i)=>r+(i&1?-v:v)*D(m.slice(1).map(r=>r.filter((a,j)=>j-i))),0):m)&&(d=b*b-4*a*c)?d<0?!b&c==a?'Circle':'Ellipse':'Hyperbola':'Parabola'

console.log=(...x)=>O.textContent+=x+'\n'

;[
 [[0, 0], [1, 5], [2, 3], [4, 8], [9, 2]]
,[[1.2, 5.3],[4.1, 5.6], [9.1, 2.5], [0, 1], [4.2, 0]]
,[[5, 0], [4, 3], [3, 4], [0, 5], [0, -5]]
,[[1, 0], [0, 1], [2, 1], [3, 4], [4, 9]]
].forEach(t=>console.log(t.join`|`+' => '+F(t)))
<pre id=O></pre>


2
真是太好了!优秀作品!
Alex A.

2

Python-234个字节

import numpy as n
x=input()
d=[n.linalg.det(n.delete(n.array([[i*i,i*j,j*j,i,j,1]for i,j in x]),k,1))for k in range(6)]
t=d[1]**2-4*d[0]*d[2]
print"hyperbola"if t>0else"parabola"if t==0else"circle"if d[1]==0and d[0]==d[2]else"ellipse"

我从来不打印circleparabola因为td[1]从不打准确0,但OP说,这是好的。


1

C,500

我的JavaScript答案已移植到C。只是看它是否可以完成。

用法:从标准输入中读取10个值

回声1 0 0 1 2 1 3 4 4 9 | 锥

输出:

抛物线

测试(ideone)

double D(m,k)double*m;{double t=0;for(int s=1,b=1,x=0;x<6;x++,b+=b)k&b||(t+=s*m[x]*(k+b>62?1:D(m+6,k+b)),s=-s);return t;}i,u,h;double m[36],*t=m+6,w[6],s[3],b,d;main(){for(;i++<5;*t++=d*d,*t++=d*b,*t++=b*b,*t++=d,*t++=b,*t++=1)scanf("%lf%lf",&d,&b);for(u=4;u;u/=2)for(m[0]=u&1,m[1]=u&2,m[2]=u&4,d=D(m,0),h=0;d&&h<3;h++){for(i=0;i<6;i++)w[i]=m[i*6+h],m[i*6+h]=i?0:u;s[h]=D(m,0)/d;for(;i--;)m[i*6+h]=w[i];}b=s[1];d=b*b-4*s[0]*s[2];puts(d?d<0?!b&(s[2]==s[0])?"Circle":"Ellipse":"Hyperbola":"Parabola");}

少打高尔夫球

// Calc determinant of a matrix of side d
// In the golfed code, d is fix to 6
double D(m, d, k)
double*m;
{
    int s = 1, b = 1, x = 0;
    double t = 0;
    for (; x < d; x++, b += b)
        k&b || (
            t += s*m[x] *(k+b+1==1<<d? 1: D(  m + d, d, k + b)), s = -s
        );
    return t;
}

double m[36],d, *t = m + 6, w[6], s[3], a, b, c;
i,u,h;
main()
{
    for (; i++ < 5; )
    {
        scanf("%lf%lf", &a, &b);
        *t++ = a*a, *t++ = a*b, *t++ = b*b, *t++ = a, *t++ = b, *t++ = 1;
    }
    for (u = 4; u; u /= 2)
    {
        m[0] = u & 1, m[1] = u & 2, m[2] = u & 4;
        d = D(m, 6, 0);
        if (d) 
            for (h = 0; h < 3; h++)
            {
                for (i = 0; i < 6; i++)
                    w[i] = m[i * 6 + h],
                    m[i * 6 + h] = i ? 0 : u;
                s[h] = D(m, 6, 0)/d;
                for (; i--; )
                    m[i * 6 + h] = w[i];
            }
    }
    a = s[0], b = s[1], c = s[2];
    d = b*b - 4 * a * c;
    puts(d ? d < 0 ? !b&(c == a) ? "Circle" : "Ellipse" : "Hyperbola" : "Parabola");
}

1

贤者,247字节

def f(p):
 for i in[1,2,4]:
  z=[i&1,i&2,i&4,0,0,0]
  M=matrix([z]+[[x*x,x*y,y*y,x,y,1]for x,y in p])
  try:A,B,C=(M\vector(z))[:3]
  except:continue
  d=B*B-4*A*C
  return['parabola','hyperbola','circle','ellipse'][[d==0,d>0,d<0and B==0and A==C,d<0].index(1)]

在线尝试

此函数采用一个可迭代的(x,y)对作为输入,尝试计算每个3个可能的线性系统(的的判别A=1B=1C=1),并输出圆锥截面的基于判别,的值的类型ABC

可能还有更多打高尔夫球的事情,但是我对Sage感到生疏,现在很困,所以我会在早上做更多的工作。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.