[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dennou-ruby:000104] NMDArray revised



��Ƿ��Ǥ���

�������Υ������å׸�� ftp �����Ȥ��֤��ޤ��� ruby �ˤ��¿
��������饤�֥�꡼�򹹿����ޤ������־�������Ʊ��

  ftp://dennou-t.ms.u-tokyo.ac.jp/arch/davis/ex/1999-09-08-horinout/

�ǡ��ե�����̾��

  nmd_array-19991004.tgz

�Ǥ���

�� ���Υǥ��쥯�ȥ꡼�Υ͡��ߥ󥰥����ƥ�Ǥϡ���������Τ��֤���
   �ˤ����������դ��ڤ�ľ�����Ȥ����Ԥ���Ƥ���褦�ʵ��������...

���󤫤���Ѳ��Ȥ����С�¿�������󥯥饹 NMDArray �˿��᥽�å�
each_set! �� each_set (non-destructive version) ���ä�ä�������
����������ϰ�������ˤʤä�������������μ�����Ǥ��Τǡ����
�Ƥ����ޤ������� ruby �������Ĥ��Ƥޤ���������򤽤ä��� C �δ�
�����֤���������С�������˥��֥������Ȥ��뤳�Ȥʤ��˥桼����
�����ꤷ������٤���������黻��Ԥ���Ȥ������Ȥˤʤ뤫�⤷���
���� --- �äƿɤ��Ǥ�����(�֥��å������ѡ������ʤ��¤�̵����)
�ʤ����ȹ��ߤ����� Array �˼�������Ƥ��� each ��Ʊ�͡��֥��å�
��Ȥ��ޤ��Τǡ�ʣ���Ԥˤ錄��黻������롢ifʸ���Ȥ���ΤDZ黻
�оݤ�����Ǥ���(fortran90��where��ʸ�ߤ�����)���Ȥ��ä����å�
������ޤ���

��Ȥϴ�ñ�ʤΤǺǸ�˰��Ѥ��ޤ�����äȤ�����ˡ����衢�Ȥ��ä�
��Ŧ�����������ꤤ���ޤ���

̤���������ʣ���Υ��֥������Ȥ˱黻��Ŭ�Ѥ��뤳�ȤǤ����ɤ���
�������Ȥ���򤢤���ȡ�fortran90 �� a, b �Ȥ���Ʊ���������󤬤���
���Ȥ���

   where(a>0)
     a=a*b
   endwhere

�ʤɤȡ�where��Ƚ�Ǥ��о�(a)�ʳ�������(b)���������黻�򤹤�
�褦�ʤ��ȤǤ���each_set�Υ֥��å�������()�ǰ������餻�뤳�Ȥ�
�����Τǡ�¾�Υ��֥������Ȥ�᥽�åɤ˰����Ȥ���Ϳ�����ޤ�����
�㤨�������Ƥ⤽�����Ǥ�ɤ��֥��å����Ȥ߹�����餤�����狼���
���󡣤���(¾�Υ��֥������ȼ�����)����褿�餤����Ǥ�����

---
  def each_set!(&block)
    # Elementwise operation. Each element is replaced with the value
    # evaluated by the last statement in the block. The data substitution
    # is suppressed when the value is nil.
    # Note that the operation can be applied to selected elements by using
    # if conditioning (see examples).
    # This is a destructive method (with "!"); the object is modified
    # directly. Use each_set (without "!") to create another object to
    # return the result, leaving the original one intact.
    #
    # Usage: array.each_set!{block}
    # Example: array.each_set!{|x| x*3}     # multiply 3 to all elements
    #          array.each_set!{|x| x*3 if x>0}    #same but only when positive
    #          array.each_set!{|x| s=sin(x); x+s if(cos(x)>0.0)}
    #                       # last value (x+s) is substituted if the condition
    #                       # is satisfied. Here, s is a local variable
    #                       # only in the block ({} introduces a new scope).

    @xxxxxx do
      |i|
      eval = block.call(@xxxxxx[i])
      @xxxxxx[i]=eval if (eval != nil)
    end
  end

  def each_set(&block)
    out=self.dclone         # deep clone
    out.each_set!(&block)
    return out
  end
---
�����㡧

% irb
irb(main):001:0> require "NMDArray"
nil
irb(main):002:0> a=NMDArray.indgen(3,2)
#<NMDArray: @xxxxxx=[3, 2], @xxxxxx=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0], @xxxxxx=6, @xxxxxx=2>
irb(main):003:0> b=a.each_set{|x| x*3 if x>3}
#<NMDArray: @xxxxxx=[3, 2], @xxxxxx=[0.0, 1.0, 2.0, 3.0, 12.0, 15.0], @xxxxxx=6, @xxxxxx=2>
irb(main):004:0> a.prt
[0.0, 1.0, 2.0,
3.0, 4.0, 5.0]
nil
irb(main):005:0> b.prt
[0.0, 1.0, 2.0,
3.0, 12.0, 15.0]
nil

---
��Ƿ�� ��                 horinout@xxxxxx
�������Ķ�������ȸ��楻�󥿡�    611-0011 �����Ըޥ���
phone:0774-38-3812                     fax:0774-31-8463