ホーム » Linux (ページ 2)

Linux」カテゴリーアーカイブ

2024年4月
 123456
78910111213
14151617181920
21222324252627
282930  

カテゴリー

アーカイブ

ブログ統計情報

  • 80,010 アクセス



Python から CPP の呼出し – 2


予めデータ(vv_PLF)を作成して,必要に応じてファイルに出力するコード.

vv_PLF*	get_vv_PLF	(void)
{
	static	vv_PLF	G_PLF ;
	return	&G_PLF ;
	}
static	PyObject*	check_rd	(PyObject* self, PyObject* args)
{
	vv_PLF*	gvv_plf = get_vv_PLF() ;
	vv_PLF	vv_plf_ = ::Check_Revise_deg() ;
	*gvv_plf = vv_plf_ ;
	return	Py_None ;
	}
static	PyObject*	dump_svg	(PyObject* self, PyObject* args)
{
	vv_PLF*	gvv_plf = get_vv_PLF() ;
	::Dump_SVG(*gvv_plf) ;
	return	Py_None ;
	}
static	PyObject*	dump_ipl	(PyObject* self, PyObject* args)
{
	vv_PLF*	gvv_plf = get_vv_PLF() ;
	::Dump_ipl(*gvv_plf) ;
	return	Py_None ;
	}

Python から C++ の呼出し 複数のメソッドで共通な領域を使用する
Python 側では
* check_rd でデータを作成.
* dump_svg などでデータを出力.


3D データを読み込んで,指定されたファイル名(拡張子により形式を判断)で出力.

static	PyObject*	load	(PyObject* self, PyObject* args)
{
	const	char*	str_file = NULL ;
	if (!PyArg_ParseTuple(args,"s",&str_file))	{
		return	NULL ;
		}
	tstring	g3_file = str_file ;
	GonsA	gnsa = ::To_GonsA(g3_file.c_str()) ;
	set_GonsA(gnsa) ;
	return	Py_None;
	}
static	PyObject*	save	(PyObject* self, PyObject* args)
{
	const	char*	str_file = NULL ;
	if (!PyArg_ParseTuple(args,"s",&str_file))	{
		return	NULL ;
		}
	tstring	g3_file = str_file ;
	GonsA*	gnsa = ::get_GonsA() ;
	::GonsA_To(*gnsa,g3_file.c_str()) ;
	return	Py_None;
	}
static	PyObject*	gons_to	(PyObject* self, PyObject* args)
{
	const	char*	str_file = NULL ;
	if (!PyArg_ParseTuple(args,"s",&str_file))	{
		return	NULL ;
		}
	tstring	g3_file = str_file ;
	gons_to(g3_file.c_str()) ;
	return	Py_None;
	}

Python から C++ の呼出し 3D データの変換

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

Python から CPP の呼出し

雰囲気はつかめてきたので,以前作成した cpp を呼んでみることに…
実際の処理部分は C++ のコードだが,呼び出しは C の関数.
また引数もない状態なので,C 関数の system(“a.out”) と呼んでいるのと同様.

#include	"i_rd_dbg.hxx"
int _tmain(int argc, TCHAR* argv[])
{
	::Test_Revise_deg() ;
	return 0 ;
	}

#include	<Python.h>
#include	"i_rd_dbg.hxx"
#include	"messbar.cxx"

static	PyObject*	call_cpp(PyObject* self, PyObject* args)
{
	::Test_Revise_deg() ;
	return	Py_None ;
	}

static	PyMethodDef 	 	myMethods[] = {
	{	"_call_cpp_",	call_cpp,	METH_NOARGS,	"call cpp"		},
	{	NULL														},
	} ;

static	struct	PyModuleDef	call_mod = {
	PyModuleDef_HEAD_INIT,
	"call_mod",
	"call_cpp module",
	-1,
	myMethods
	} ;

PyMODINIT_FUNC	PyInit_call_mod(void)
{
	return	PyModule_Create(&call_mod) ;
	}

コンパイルして import まではできたが,メソッドをうまく呼び出せない.
>>> call_mod.call_cpp()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module ‘call_mod’ has no attribute ‘call_cpp’
>>>
それで,メソッドを表示できないかと思い検索すると,
python でメソッドの一覧を取得する方法
他に dir(call_mod) もあった.

Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc $ g++ rvs_sc_w.cpp -o call_mod.so  -fPIC -Wall -shared -I /volume1/.@plugins/AppCentral/python3/include/python3.7m/
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc $
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc $ python3
Python 3.7.0 (default, Aug 23 2018, 17:48:39)
[GCC 4.6.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import call_mod
>>> call_mod.call_cpp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'call_mod' has no attribute 'call_cpp'
>>> call_mod.call_cpp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'call_mod' has no attribute 'call_cpp'
>>> obj = call_mod
>>> import inspect
>>> for m in inspect.getmembers(obj):
...     print(m)
...
('__doc__', 'call_cpp module')
('__file__', '/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc/call_mod.so')
('__loader__', <_frozen_importlib_external.ExtensionFileLoader object at 0x7f5a1a35ec50>)
('__name__', 'call_mod')
('__package__', '')
('__spec__', ModuleSpec(name='call_mod', loader=<_frozen_importlib_external.ExtensionFileLoader object at 0x7f5a1a35ec50>, origin='/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc/call_mod.so'))
('_call_cpp_', <built-in function _call_cpp_>)
>>> call_mod._call_cpp_()
>>>

Python から cpp の呼出し メソッドの表示

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

Python から C の呼出し

C のコードを Python から呼出せないかと…
Python のドキュメントとしては次の所にある
C や C++ による Python の拡張

Win10      C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\include\Python.h
debian10   /usr/include/python3.7/Python.h
AS5202T    /volume1/.@plugins/AppCentral/python3/include/python3.7m/Python.h

Iwao@AS5202T:/volume1/.@plugins/AppCentral/python3/include/python3.7m $ find / -name Python.h
/volume1/.@plugins/AppCentral/linux-center/containers/debian10/rootfs/usr/include/python2.7/Python.h
/volume1/.@plugins/AppCentral/linux-center/containers/debian10/rootfs/usr/include/python3.7m/Python.h
/volume1/.@plugins/AppCentral/python/include/python2.7/Python.h
/volume1/.@plugins/AppCentral/python3/include/python3.7m/Python.h

検索 して見つけたもの.
https://www.fsi-embedded.jp/kumico/columns/?cat=python
https://qiita.com/donkonishi/items/b7825b34d0711e336c61
https://www.quark.kj.yamagata-u.ac.jp/~hiroki/python/?id=19
http://owa.as.wakwak.ne.jp/zope/docs/Python/BindingC/
https://cpp-learning.com/?s=”Python+C+API”


Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $ cat hellWrap.c
#include <Python.h>
extern int add(int, int);
extern void out(const char*, const char*);

PyObject* hello_add(PyObject* self, PyObject* args)
{
    int x, y, g;

    if (!PyArg_ParseTuple(args, "ii", &x, &y))
        return NULL;
    g = add(x, y);
    return Py_BuildValue("i", g);
}

PyObject* hello_out(PyObject* self, PyObject* args, PyObject* kw)
{
    const char* adrs = NULL;
    const char* name = NULL;
    static char* argnames[] = {"adrs", "name", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kw, "|ss",
            argnames, &adrs, &name))
        return NULL;
    out(adrs, name);
    return Py_BuildValue("");
}

static PyMethodDef hellomethods[] = {
    {"add", hello_add, METH_VARARGS},
    {"out", hello_out, METH_VARARGS | METH_KEYWORDS},
    {NULL}
};

void initchello(){
  Py_InitModule("hello", hellomethods);
}

Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $ gcc -fPIC -Wall -c -o hellWrap.o hellWrap.c -I /volume1/.@plugins/AppCentral/python/include/python2.7/
hellWrap.c:30:13: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
     {"out", hello_out, METH_VARARGS | METH_KEYWORDS},
             ^~~~~~~~~
hellWrap.c:30:13: note: (near initialization for 'hellomethods[1].ml_meth')
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $ gcc -fPIC -Wall -c -o hellWrap.o hellWrap.c -I /volume1/.@plugins/AppCentral/python/include/python2.7/
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $   

Python から C の呼出し ラッパー
PyMethodDef の所でエラーとなっていていろいろと探すと,型を指定しているものがあり,それを指定.
https://bty.sakura.ne.jp/wp/archives/83

static PyMethodDef hellomethods[] = {
    {"add", (PyCFunction)hello_add, METH_VARARGS},
    {"out", (PyCFunction)hello_out, METH_VARARGS | METH_KEYWORDS},
    {NULL}
};

コンパイルは通る様になった.
Python から試そうとすると …

Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ gcc -fPIC -Wall -c -o helloWrap.o helloWrap.c -I /volume1/.@plugins/AppCentral/python/include/python2.7/
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ gcc -fPIC -Wall -shared -o hellomodule.so hello.o helloWrap.o
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ ll
total 44
drwxrwxrwx    3 Iwao     users       4.0K Jul 29 18:16 ./
drwxrwxrwx    5 Iwao     users       4.0K Jul 29 15:31 ../
drwxrwxrwx    2 Iwao     users       4.0K Jul 29 18:05 bak/
-rwxrwxrwx    1 Iwao     users        188 Jul 29 16:29 hello.c*
-rw-r--r--    1 Iwao     users       1.6K Jul 29 16:33 hello.o
-rwxrwxrwx    1 Iwao     users        910 Jul 29 16:32 helloWrap.BAK*
-rwxrwxrwx    1 Iwao     users        936 Jul 29 16:37 helloWrap.c*
-rw-r--r--    1 Iwao     users       3.1K Jul 29 18:16 helloWrap.o
-rwxr-xr-x    1 Iwao     users       8.3K Jul 29 18:16 hellomodule.so*
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ python3
Python 3.7.0 (default, Aug 23 2018, 17:48:39)
[GCC 4.6.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'hello'
>>> exit()
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ python
Python 2.7.10 (default, Aug 19 2015, 09:18:54)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (inithello)
>>> exit()
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $   

Python から C の呼出し import でエラー
まだ何か違うみたい.
メッセージは inithello がないとなっているので helloWrap.c を見直すと…
void initchello() { … } となっている.
関数名を inithello に変更してビルドすると通った.
Python から C の呼出し


2020/07/30
今日は次の所を参考にさせてもらって…
https://cpp-learning.com/python_c_api_step1/

Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ gcc ph_hello.c -o mymodule.so -fPIC -Wall -shared -I /volume1/.@plugins/AppCentral/python3/include/python3.7m/
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ python3
Python 3.7.0 (default, Aug 23 2018, 17:48:39)
[GCC 4.6.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymodule
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_mymodule)
>>> import myModule
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myModule'
>>>
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ cp mymodule.so myModule.so
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ ll
total 44
drwxrwxrwx    3 Iwao     users       4.0K Jul 30 16:49 ./
drwxrwxrwx    6 Iwao     users       4.0K Jul 30 14:59 ../
drwxrwxrwx    2 Iwao     users       4.0K Jul 30 16:48 bak/
-rwxr-xr-x    1 Iwao     users       8.1K Jul 30 16:49 myModule.so*
-rwxr-xr-x    1 Iwao     users       8.1K Jul 30 16:47 mymodule.so*
-rwxrwxrwx    1 Iwao     users        188 Jul 29 16:29 ph_hello.BAK*
-rwxrwxrwx    1 Iwao     users       1.3K Jul 30 16:27 ph_hello.c*
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ python3
Python 3.7.0 (default, Aug 23 2018, 17:48:39)
[GCC 4.6.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import myModule
>>> myModule.helloworld
<built-in function helloworld>
>>> myModule.helloworld()
Hello World
>>>
>>>
>>>
>>>
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $

Python から C の呼出し  py_hello
コンパイル時の出力ファイル名 mymodule.so が間違っていた.正しくは myModule.so .


そこ には詳しく書かれているが,自分用にメモ.
PyMethodDef の “メソッド名” は Python 側での (モジュール名).(メソッド名) .
同様に PyModuleDef の “モジュール名” は import 時の名称..so の出力ファイル名も対応している必要がある?
文字列なので,異なっていてもコンパイル時のエラーにはならない.
実行時に見つからないなどのエラーとなる.


2020/07/31
続きの内容をやっていて…
https://cpp-learning.com/python_c_api_step2/
Python から C の呼出しでエラー
Fatal Python error: GC object already tracked
Python から C の呼出し  Py_DECREF をコメントに
c_list は Python 側で確保しているため Py_DECREF はうまくないのではないか?


https://jml.mish.work/python/call-c-python.html

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

Python 負のインデックス

次の所を読ませてもらっていて感じたことを…
けいしゅけのブログ薬局 情報館
https://keisyuke-blogyakkyoku.xyz/python-list-index
今私が習得するのにタイミングや更新サイクルが丁度良いので助かっている.


ASUSTOR NAS AS5202T に SSH 接続して Python を操作.

C:\Program Files\Microsoft Office\Office14>cd C:\Users\Iwao\AppData\Local\Temp

C:\Users\Iwao\AppData\Local\Temp>ssh -l Iwao -p 22 192.168.1.75
Password:
Iwao@AS5202T:/volume1/home/Iwao $ python
Python 2.7.10 (default, Aug 19 2015, 09:18:54)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[0,1,2,3,4]
>>> print(a[0])
0
>>> print(a[4])
4
>>> print(a[5])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> print(a[-1])
4
>>> print(a[-4])
1
>>> print(a[-5])
0
>>> print(a[-6])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>>  

Python 負のインデックス
似たようなことを C++ でやろうとすると次の様な感じ.
std::cout << a[a.size()-1] ;


この様な記述ができるからか,今まで負のインデックスの必要性は感じたことがなかった.
また配列の検索で該当するものがないと -1 を返す様なコードもよく書く.
インデックスを変数で持つ時は?
いろいろ考えると個人的にはあまり使わないのではないかと思う.
Python などをやればもっと有効な使い方が出てくるのか?

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

GLUT でのメニュー

GLUT を使用してのテストコードで,起動後データを切替える方法がないかと…
メニューの利用で何とかできるか?


glutCreateMenu などで検索したがわかりやすい情報が少なかった.
https://seesaawiki.jp/w/mikk_ni3_92/d/%b4%f0%cb%dc%ca%d418
https://www.jstage.jst.go.jp/article/itej/67/5/67_417/_pdf
http://opengl.jp/glut/section06.html


以前作成した雛型に対して追加.メニュー部分はほぼリンク先のコードのまま.
https://jml.mish.work/index.php/cpp/cb-glut.html

#include	"glut_cb.hxx"
#include	<iostream>

void	cb_menu	(int val)
{
	std::cout << "menu val=" << val << std::endl ;
	}

int	main(int argc, char* argv[])
{
	::glutInitWindowPosition(200,100) ;
	::glutInitWindowSize	(600,400) ;
	::glutInitDisplayMode	(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) ;
	::glutInit           	(&argc,argv) ;
	::glutCreateWindow   	(argv[0]) ;
	::glutReshapeFunc    	(cb_resize) ;
	::glutDisplayFunc     	(cb_display) ;
	::glutKeyboardFunc    	(cb_keyboard) ;
	::glutMouseFunc      	(cs_mouse) ;
	{
		::glutCreateMenu(cb_menu) ;
		::glutAddMenuEntry("name 1",1) ;
		::glutAddMenuEntry("name 2",2) ;
		::glutAddMenuEntry("name 3",3) ;
		::glutAttachMenu(GLUT_RIGHT_BUTTON) ;
		}
	::cb_init            	() ;
	::glutMainLoop      	() ;
	return	0 ;
	}

GLUT メニュー
結局はこの方法ではなく,予めリスト化して キー入力 により切り替える方法に.

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

Debian 環境に pip のインストール

PyOpenGL を使おうとして pip コマンドを打つと,コマンドがない.
どうも pip が入っていないみたいで,次の様に入力してインストール.
sudo apt install python-pip
更に PyOpenGL のインストール.
pip install PyOpenGL
Debian 環境に PyOpenGL のインストール


pip3 のインストールは
sudo apt install python3-pip


C:\WINDOWS\System32>cd C:\Users\Iwao\AppData\Local\Temp

C:\Users\Iwao\AppData\Local\Temp>ssh -l admin -p 22 lxcdebian10
admin@lxcdebian10's password:
Linux lxcdebian10 4.14.x #1 SMP Wed May 13 00:37:48 CST 2020 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Jul  9 10:51:02 2020 from fe80::ed6f:4991:21c9:1882%eth0
admin@lxcdebian10:~$ cat /usr/bin/pip
#!/usr/bin/python
# GENERATED BY DEBIAN

import sys

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip._internal import main
if __name__ == '__main__':
    sys.exit(main())
admin@lxcdebian10:~$ cat /usr/bin/pip3
#!/usr/bin/python3
# GENERATED BY DEBIAN

import sys

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip._internal import main
if __name__ == '__main__':
    sys.exit(main())
admin@lxcdebian10:~$  

Debian  cat pip , pip3

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

AS5202T Debian 10 に Image Magick

Raspberry Pi などと同様
sudo apt install imagemagick
Debian 環境に Image Magick のインストール
特に難しいことはない.
Debian 環境に Image Magic インストール後 display コマンド

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

AS5202T Debian 10 と VC

VC で ASUSTOR NAS Linux Center の Debian 10 Desktop を使用するための設定.


SSH 接続を可能にして gcc などをインストールしておく必要がある.
Linux development with C++ in Visual Studio


VC の「ツール」-「オプション」,「クロスプラットフォーム」-「接続マネージャー」-「追加」.
Debian 10 への接続の設定
SSH 接続する時の情報を設定する.
追加後「接続マネージャー」-「リモートヘッダー…」で「更新」するとエラー.
エラーが発生しました。Could not start the ‘rsync’ command on the remote host, please install it using your system package manager. Please see https://aka.ms/AA23jat for troubleshooting。詳細については、C:\Users\Iwao\AppData\Local\Temp\vslinux_header_update_log.txt を参照してください。トラブルシューティングを行うには、https://aka.ms/AA23jat をご覧ください。
エラーが発生しました。Could not start the 'rsync' command on the remote host, please install it using your system package manager.
この部分の対応方法はよくわからない.


VC のプロジェクトの設定を「lxcdebian10」に.
VC リモートビルドマシンを「lxcdebian10」に設定


これでビルドすれば大丈夫なはずだが,何故かエラー.
VC を再起動したり,プロジェクトを読み直したりしていたら通る様になった.
AS5202T Debuan 10 VC ビルド

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

AS5202T Debian 10

以前 インストールしてそのままになっていた環境.
その時わからなかったのが「タイムゾーン」の設定.
次の所を参考にさせてもらって設定.
Debian 10 busterをインストールして最初に行う設定と確認 10項目
Linux Center Debian 10 タイムゾーンの設定


共通のソースなどを参照するために…
Linux から Windows 環境への接続
Linux Center Debian 10 mount
接続先は,最終的には変更する予定.

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 11

JavaScript 版で Canvas を使用する様にしてみた.
弦と矢と半径 JavaScript Canvas
https://mish.myds.me/Test/blog/r_cs/b/

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 10

JavaScript 数値入力で更新.

	<body>

<pre>
c : <input  type='number'  id='in__c'  value='8'  style='width:100px;'  step='any' /> <span  id='out_c'> </span>  <br />
s : <input  type='number'  id='in__s'  value='2'  style='width:100px;'  step='any' /> <span  id='out_s'> </span>  <br />
r : <input  type='number'  id='in__r'  value='5'  style='width:100px;'  step='any' /> <span  id='out_r'> </span>  <br />
</pre>
		<img	src='r_cs.png'	width='300px'>

		<script>

			function	r_cs	(c , s)		{	return	(        ( c*c )  / ( 8*s )  +  s/2 ) ;		}
			function	s_rc	(r , c)		{	return	( r-Math.sqrt(r*r - (c/2)*(c/2) )   ) ;		}
			function	c_rs	(r , s)		{	return	(   Math.sqrt(r*r - (r-s)*(r-s) )*2 ) ;		}

			function	f_c_rs	() {
				var	oc = document.querySelector("#out_c") ;
				var	os = document.querySelector("#out_s") ;
				var	or = document.querySelector("#out_r") ;
				var	ic = document.querySelector("#in__c") ;
				var	is = document.querySelector("#in__s") ;
				var	ir = document.querySelector("#in__r") ;
				var	fc = parseFloat(ic.value) ;
				var	fs = parseFloat(is.value) ;
				var	fr = parseFloat(ir.value) ;
				oc.textContent = "" ;
				os.textContent = "" ;
				or.textContent = "" ;
				os.textContent = s_rc(fr,fc) ;
				or.textContent = r_cs(fc,fs) ;
				}
		//	...
			{
				var	ec = document.getElementById("in__c") ;
				var	es = document.getElementById("in__s") ;
				var	er = document.getElementById("in__r") ;
				ec.addEventListener("keyup",f_c_rs, false) ;
				es.addEventListener("keyup",f_s_rc, false) ;
				er.addEventListener("keyup",f_r_cs, false) ;
				}

			</script>

		</body>

弦と矢と半径 JavaScript 版
https://mish.myds.me/…/r_cs/a/

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 9

JavaScript で.

<body>
	c : <input	type='number'	id='in__c'	value='80.0'	size='10'	step='any'	/><input	type='submit'	value='c_rs'	id='c_rs'	/>	<span	id='out_c'> </span>	<br />
	s : <input	type='number'	id='in__s'	value='20.0'	size='10'	step='any'	/><input	type='submit'	value='s_rc'	id='s_rc'	/>	<span	id='out_s'> </span>	<br />
	r : <input	type='number'	id='in__r'	value='50.0'	size='10'	step='any'	/><input	type='submit'	value='r_cs'	id='r_cs'	/>	<span	id='out_r'> </span>	<br />
	<img	src='r_cs.png'	width='300px'>

	<script>
		function	r_cs	(c , s)		{	return	(        ( c*c )  / ( 8*s )  +  s/2 ) ;		}
		function	s_rc	(r , c)		{	return	( r-Math.sqrt(r*r - (c/2)*(c/2) )   ) ;		}
		function	c_rs	(r , s)		{	return	(   Math.sqrt(r*r - (r-s)*(r-s) )*2 ) ;		}

		function	f_c_rs	() {
			var	oc = document.querySelector("#out_c") ;
			var	os = document.querySelector("#out_s") ;
			var	or = document.querySelector("#out_r") ;
			var	ic = document.querySelector("#in__c") ;
			var	is = document.querySelector("#in__s") ;
			var	ir = document.querySelector("#in__r") ;
			var	fc = parseFloat(ic.value) ;
			var	fs = parseFloat(is.value) ;
			var	fr = parseFloat(ir.value) ;
			oc.textContent = "" ;
			os.textContent = "" ;
			or.textContent = "" ;
			oc.textContent = c_rs(fr,fs) ;
			}
		//	...
		{
			var	ec = document.getElementById("c_rs") ;
			var	es = document.getElementById("s_rc") ;
			var	er = document.getElementById("r_cs") ;
			ec.addEventListener("click",f_c_rs, false) ;
			es.addEventListener("click",f_s_rc, false) ;
			er.addEventListener("click",f_r_cs, false) ;
			}
		</script>
	</body>

R S C JavaScript
https://mish.myds.me/…/r_cs/9/

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 8

Python を使用しないで PHP のみで.

<?php
	function	r_cs	($c , $s)	{	return	(    ( $c*$c )  / ( 8 *$s )   +  $s/2 ) ;	}
	function	s_rc	($r , $c)	{	return	( $r-sqrt($r*$r - ($c/2)*($c/2) )     ) ;	}
	function	c_rs	($r , $s)	{	return	(    sqrt($r*$r - ($r-$s)*($r-$s) )*2 ) ;	}
	?>

PHP のみで実装
https://mish.myds.me/…/r_cs/8/

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 7

input タグで number を使用している部分.
step=’0.001′ などとすれば小数値を入力できるが,欲しい動作ではない.
例えば 1.23456789 など任意の値を入力可能にしたかった.
step=’any’ と指定することで小数部を入力できるようになる.
<input type=’number’ name=’c’ value=’1.234567890′ size=’15’ step=’any’ />
https://mish.myds.me/…/r_cs/7/
弦と矢と半径 7

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 6

Python のコードを修正して「結果のみ」を出力する様に変更.

import	sys
import	math

def	r_cs	(c , s)	:	return	(        ( c*c )  / ( 8 *s ) +  s/2 )
def	s_rc	(r , c)	:	return	( r-math.sqrt(r*r - (c/2)*(c/2) )   )
def	c_rs	(r , s)	:	return	(   math.sqrt(r*r - (r-s)*(r-s) )*2 )

cmd =       sys.argv[1]
p1  = float(sys.argv[2])
p2  = float(sys.argv[3])

#print	(cmd + " " + str(p1) + " " + str(p2))

if  	cmd == "r_cs"	:	print	(r_cs(p1,p2))
elif	cmd == "s_rc"	:	print	(s_rc(p1,p2))
elif	cmd == "c_rs"	:	print	(c_rs(p1,p2))
#else	            	:	print	("error")

#print	("")

弦と矢と半径 スマートフォンからアクセス
PHP
あまり綺麗なコードでないので,ここには張り付けていません.リンク先を見てください.
PHP から Python の呼び出し
https://mish.myds.me/…/r_cs/6/

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 5

PHP から Python の呼び出しで,Python のコマンドライン引数を使用する.

<?php

	echo	("call python\n") ;

	echo	("\n") ;
	system	("python r_cs.py r_cs 6 1") ;
	system	("python r_cs.py s_rc 5 6") ;
	system	("python r_cs.py c_rs 5 1") ;

	echo	("\n") ;
	system	("python r_cs.py r_cs 8 2") ;
	system	("python r_cs.py s_rc 5 8") ;
	system	("python r_cs.py c_rs 5 2") ;

	?>

Python のコードは,

import	sys
import	math

def	r_cs	(c , s)	:	return	(        ( c*c )  / ( 8 *s ) +  s/2 )
def	s_rc	(r , c)	:	return	( r-math.sqrt(r*r - (c/2)*(c/2) )   )
def	c_rs	(r , s)	:	return	(   math.sqrt(r*r - (r-s)*(r-s) )*2 )

cmd =       sys.argv[1]
p1  = float(sys.argv[2])
p2  = float(sys.argv[3])

print	(cmd + " " + str(p1) + " " + str(p2))

if  	cmd == "r_cs"	:	print	(r_cs(p1,p2))
elif	cmd == "s_rc"	:	print	(s_rc(p1,p2))
elif	cmd == "c_rs"	:	print	(c_rs(p1,p2))
else	            	:	print	("error")

print	("")

Python がよくわかっていないので,いろいろなエラーが…
PHP から Python の呼び出し コマンドライン引数
https://mish.myds.me/…/r_cs/5/

弦,矢,半径

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 4

先日の Python コードのバグ(整数で計算されてしまうため結果が異なる).
原因はすぐわかるが,対応は悩む所.
コードを分解して動作を確認してみた.

def	r_cs_o	(c , s)	:	return	(        ( c*c )  / ( 8 *s )  +  s/2  )
def	r_cs_n	(c , s)	:	return	(        ( c*c )  / ( 8.*s )  +  s/2. )

print   (6*6)
print   (8*1)

print   (36/8)
print   (36/8.)

print   (1/2)
print   (1/2.)

print	("")
print   (36/8 +1/2 )
print   (36/8.+1/2.)

print	("")
print	(r_cs_o(6,1))
print	(r_cs_n(6,1))

Python での「浮動小数点数」の動作テスト
https://docs.python.org/ja/3/howto/pyporting.html?highlight=除算


Synology NAS DSM では Python 2.7 が標準で入っているみたい.
「パッケージ センター」で Python3 を追加できる.

C:\Program Files\Microsoft Office\Office14>cd C:\Users\Iwao\AppData\Local\Temp

C:\Users\Iwao\AppData\Local\Temp>ssh -l Iwao -p 2200 192.168.1.116
Iwao@192.168.1.116's password:
Iwao@DS116:~$ python3
Python 3.5.1 (default, Jan 29 2018, 14:16:30)
[GCC 4.9.3 20150311 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Iwao@DS116:~$ python2
Python 2.7.12 (default, May 12 2020, 04:48:57)
[GCC 4.9.3 20150311 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Iwao@DS116:~$ python
Python 2.7.12 (default, May 12 2020, 04:48:57)
[GCC 4.9.3 20150311 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Iwao@DS116:~$

Synology NAS DS116  python コマンド
ASUSTOR NAS ADM では入ってない.「App Central」でインストール可能.
ASUSTOR NAS Python


2020/11/20
QNAP NAS での Python3 は,他の NAS と異なるみたい.
opkg install python3 でインストールしなければならなかった.
https://mish.myds.me/wordpress/dev/2020/08/29/ts253d-setup-3-opkg/
https://mish.myds.me/wordpress/dev/2020/08/17/ts-253d-setup-7-python3/

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 3

JavaScript は html に埋め込んだ.

<!DOCTYPE html>
<html	lang="ja">
	<head>
		<meta	charset="UTF-8"	/>
		<meta	name="viewport"   	content="width=device-width,initial-scale=1.0">
		<title	>JavaScript</title>
		</head>
	<body>
		<script>

			function	r_cs	(c , s)		{	return	(        ( c*c )  / ( 8*s )  +  s/2 ) ;		}
			function	s_rc	(r , c)		{	return	( r-Math.sqrt(r*r - (c/2)*(c/2) )   ) ;		}
			function	c_rs	(r , s)		{	return	(   Math.sqrt(r*r - (r-s)*(r-s) )*2 ) ;		}

			document.write	("6 1 5") ; 	document.write	("<br/>\r\n") ;
			document.write	(r_cs(6,1)) ;	document.write	("<br/>\r\n") ;
			document.write	(s_rc(5,6)) ;	document.write	("<br/>\r\n") ;
			document.write	(c_rs(5,1)) ;	document.write	("<br/>\r\n") ;
			document.write	("") ;      	document.write	("<br/>\r\n") ;

			document.write	("8 2 5") ; 	document.write	("<br/>\r\n") ;
			document.write	(r_cs(8,2)) ;	document.write	("<br/>\r\n") ;
			document.write	(s_rc(5,8)) ;	document.write	("<br/>\r\n") ;
			document.write	(c_rs(5,2)) ;	document.write	("<br/>\r\n") ;
			document.write	("") ;      	document.write	("<br/>\r\n") ;

			</script>
		</body>
	</html>

動作は,
https://mish.myds.me/…/r_cs/3/
html に JavaScript を埋め込み

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径 – 2

今度は PHP からの呼び出し.

<?php

	echo	("call c++\n") ;
	system	("./a.out") ;

	echo	("call python\n") ;
	system	("python r_cs.py") ;

	?>

実際の動作を試すには,gcc と python が必要です.
cpp のコンパイルは
g++ -Wall r_cs.cpp
これで ./a.out が作成されます.
python の呼び出しは
python r_cs.py
r_cs.cpp のコンパイルと r_cs.py の実行
次の所で,同じ様な結果を表示します.
https://mish.myds.me/…/r_cs/2/

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

弦と矢と半径

次の様な計算を何かに使えないかと…

/*
https://ja.wikipedia.org/wiki/矢_(幾何学)
https://en.wikipedia.org/wiki/Circle#Sagitta
弦 c と 矢 s から 半径 r を求める
r  =	(c*c) / (8*s) + s/2

半径 r と 弦 c から 矢 s を求める
s  =	r - sqrt( r*r - (c/2)*(c/2) )

半径 r と 矢 s から 弦 c を求める
c  =	sqrt( r*r - (r-s)*(r-s) ) * 2

i_func.hxx
https://drive.google.com/file/d/1kHadFbhUi9QfGRovMXKYr2xHgcFJAqdZ/view
*/

double	r_cs	(const double c , const double s)	{	return	(   ( c*c )  / ( 8*s )  +  s/2 ) ;	}
double	s_rc	(const double r , const double c)	{	return	( r-sqrt(r*r - (c/2)*(c/2) )   ) ;	}
double	c_rs	(const double r , const double s)	{	return	(   sqrt(r*r - (r-s)*(r-s) )*2 ) ;	}

弦 矢 半径
動作を確認するために 3,4,5 や 5,12,13 は知っていたが次のキーワードで検索.
三平方の定理 整数 組み合わせ
https://ja.wikipedia.org/wiki/ピタゴラスの定理


先ず C++ で書いたもの.

#include	<cmath>

double	r_cs	(const double c , const double s)	{	return	(   ( c*c )  / ( 8*s )  +  s/2 ) ;	}
double	s_rc	(const double r , const double c)	{	return	( r-sqrt(r*r - (c/2)*(c/2) )   ) ;	}
double	c_rs	(const double r , const double s)	{	return	(   sqrt(r*r - (r-s)*(r-s) )*2 ) ;	}

#include	<iostream>

int main(void){
	std::cout << "6 1 5"   << std::endl ;
	std::cout << r_cs(6,1) << std::endl ;
	std::cout << s_rc(5,6) << std::endl ;
	std::cout << c_rs(5,1) << std::endl ;
	std::cout << std::endl ;

	std::cout << "8 2 5"   << std::endl ;
	std::cout << r_cs(8,2) << std::endl ;
	std::cout << s_rc(5,8) << std::endl ;
	std::cout << c_rs(5,2) << std::endl ;
	std::cout << std::endl ;

	return  0 ;
	}

次の所に貼り付けて動作確認できます.
https://paiza.io/ja/
https://wandbox.org/
https://ideone.com/


次は JavaScript

function	r_cs	(c , s)		{	return	(        ( c*c )  / ( 8*s )  +  s/2 ) ;		}
function	s_rc	(r , c)		{	return	( r-Math.sqrt(r*r - (c/2)*(c/2) )   ) ;		}
function	c_rs	(r , s)		{	return	(   Math.sqrt(r*r - (r-s)*(r-s) )*2 ) ;		}


	console.log	("6 1 5") ;
	console.log	(r_cs(6,1)) ;
	console.log	(s_rc(5,6)) ;
	console.log	(c_rs(5,1)) ;
	console.log	("") ;

	console.log	("8 2 5") ;
	console.log	(r_cs(8,2)) ;
	console.log	(s_rc(5,8)) ;
	console.log	(c_rs(5,2)) ;
	console.log	("") ;

Python

import	math

def	r_cs	(c , s)	:	return	(        ( c*c )  / ( 8.*s ) + s/2. )
def	s_rc	(r , c)	:	return	( r-math.sqrt(r*r - (c/2)*(c/2) )   )
def	c_rs	(r , s)	:	return	(   math.sqrt(r*r - (r-s)*(r-s) )*2 )

print	("6 1 5")
print	(r_cs(6,1))
print	(s_rc(5,6))
print	(c_rs(5,1))
print	("")

print	("8 2 5")
print	(r_cs(8,2))
print	(s_rc(5,8))
print	(c_rs(5,2))
print	("")

2020/05/30
r_cs.py の r_cs で「浮動小数点数」として扱われていなかったので修正.
def r_cs (c , s) : return ( ( c*c ) / ( 8.*s ) + s/2. )

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.